在没有文档类型的情况下在 Umbraco 7.6 中制作自定义 URL

Making custom URLs in Umbraco 7.6 without document type

我有什么

我正在使用 Umbraco 7.6.6 制作网站,我想访问我客户的第三方数据库。为此,我制作了一个名为 QuestionsController 的自定义控制器。我在其中创建了一个动作:

public class QuestionsController : SurfaceController
{
    private QuestionService _questionService = new QuestionService();

    [HttpGet]
    public ActionResult Index()
    {
        return PartialView("~/Views/MacroPartials/Questions.cshtml", _questionService.ReadFile());
    }
}

此页面索引页面工作正常,并在我的文档类型视图中由此代码调用:

Html.RenderAction("index", "Questions");

概览页面(只是一张图片):

这是我创建的模型

public class Question
{
    public int ID { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public User User { get; set; }
    public DateTime Created { get; set; }
    public List<Comment> Comments { get; set; }
}

我会怎样

现在我将创建一个详细信息页面,在正面显示更多信息。

[HttpGet]
[Route("~/questions/details/{id}")]
public ActionResult Details(int id)
{
    return View(_questionService.ReadFile().ElementAt(id));
}

重定向到该页面我会这样做:

<a href="~/questions/details/@q.ID">@q.Title</a>

但这根本行不通。详细信息页面给我一个 404 (未找到页面) 错误。

问题

我现在的问题是:如何在没有文档类型的情况下创建像 ~/questions/details/{id} 这样的自定义 URL?谁能帮帮我?

这里有一份指南:

第 1 步:添加控制器

制作你的控制器并从 Controller 扩展而不是 SurfaceController 和 return 一个 JsonResult 而不是 ActionResult:

public class QuestionsController : Controller
{
    private QuestionService _questionService = new QuestionService();

    [HttpGet]
    public JsonResult Index()
    {
        return Json(_questionService.ReadFile());
    }
}

第 2 步:添加保留路径

将控制器路径添加到 web.config 文件中的 umbracoReservedPaths appSettings 键。 Umbraco 的路由引擎将忽略添加到此列表的任何路径。

<add key="umbracoReservedPaths" value="~/umbraco,~/install/,~/questions/" />

第 3 步:更改或添加 global.asax.cs

更改或添加您的 global.asax.cs 以继承自 UmbracoApplication。打开您的 global.asax.cs 文件并更改此行:

public class MvcApplication : System.Web.HttpApplication

对此:

public class MvcApplication : UmbracoApplication

第 4 步:更改 global.asax

将您的 global.asax 更改为从您的应用程序继承。这是抓住我的大陷阱,我不得不做很多搜索才能找到它!打开您的 Global.asax 文件(右键单击它并 查看标记)并更改此行:

<%@ Application Codebehind="Global.asax.cs" Inherits="Umbraco.Web.UmbracoApplication" Language="C#" %>

对此:

<%@ Application Codebehind="Global.asax.cs" Inherits="MyProject.MvcApplication" Language="C#" %>

本例中的 MyProject 是项目的基础命名空间。

第 5 步:删除默认值 RouteConfig.cs

RouteConfig.cs 中删除默认自定义路由(如果存在)。如果您不删除此路由,MVC 将尝试路由每个请求,您的网站将显示空白页面,这可能是一个不错的干净设计,但不是很实用!

第 6 步:注册自定义路由

RouteConfig.csRegisterRoutes 方法中注册您的自定义路由。如果此文件不存在,请在文件夹 App_Start 下创建一个。您必须为要使用的每条路线执行此操作:

routes.MapRoute("Default", "{controller}/{action}/{id}", new { 
    controller = "Questions", 
    action = "Index", 
    id = UrlParameter.Optional 
});

第 7 步:添加 OnApplicationStarted

最后,您需要向 global.asax.cs 添加一个 OnApplicationStarted 覆盖方法。这将允许您添加您的应用程序以在启动时读取 RouteConfig.cs 文件中的 RegisterRoutes 方法,并将您刚刚设置的自定义路由添加到您的应用程序中:

protected override void OnApplicationStarted(object sender, EventArgs e)
{
    base.OnApplicationStarted(sender, e);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
}

第 8 步:代码清理

  1. 完成此步骤后,删除下一​​行和文件。这个你不再需要了。

    Html.RenderAction("index", "Questions");
    
  2. 为每个操作创建视图。您可以像经典 MVC 应用程序那样执行此操作。

来源:maffrigby.com (using custom routes in umbraco)