简体   繁体   English

创建自定义路由层次结构ASP.NET MVC 5

[英]Creating a custom route hierarchy ASP.NET MVC 5

My current url routing is: 我当前的网址路由是:

website.com/start/id
website.com/upload/id

These routes are wizard steps. 这些路线是向导步骤。 My controllers are named StartController, UploadController and my View structure is ~Views/Start/Index.cshtml, ~/Views/Upload/Index.cshtml 我的控制器名为StartController, UploadController ,我的View结构为StartController, UploadController ~Views/Start/Index.cshtml, ~/Views/Upload/Index.cshtml

The client has requested that he wants the url structure to be: 客户端已要求他希望url结构为:

website.com/build/start/id
website.com/build/upload/id

Wishes: 愿望:

  • I do not want use a " vanity url " because I want to use @Html.ActionLink and @Url.Action to render my urls whenever possible so I can rename controllers and controller methods later and have those "connected" @Html.ActionLink and @Url.Action update. 我不想使用“ 虚假网址 ”,因为我想尽可能使用@ Html.ActionLink和@ Url.Action呈现我的网址,以便以后可以重命名控制器和控制器方法,并让它们“连接” @ Html.ActionLink和@ Url.Action更新。

  • I do want to have all my methods in a single parent BuildContoller that references a nested View folder structure having my controller methods returing the following views: return View("~/Views/Build/Start/Index.cshtml") . 我确实希望将所有方法都放在一个父级BuildContoller中,该父类引用一个嵌套的View文件夹结构,该文件夹的控制器方法可以重现以下视图: return View("~/Views/Build/Start/Index.cshtml") I want to be able to have my View physical structure map/match to my controller naming conventions so return View(); 我希望能够将View的物理结构映射/匹配到我的控制器命名约定,因此return View(); works in controller methods. 在控制器方法中工作。

My Question: 我的问题:

Is there way that I can respect my "Wishes" above and achieve this custom Url Structure without having to create a BuildController that contains both my StartController methods and my UploadController methods? 有没有办法我可以尊重上面的“愿望”并实现此自定义网址结构,而不必创建同时包含StartController方法和UploadController方法的BuildController?

I have no problem creating a BuildController or changing the physical structure of my View folders and or controllers but I want to respect the ability of @Url.Action("Start", "Build") and not have to write "manually" in my code " build/start " and, more importantly, keep my StartController and UploadController code because I do not want a long massive bunch of code in my BuildController. 我可以轻松地创建BuildController或更改View文件夹和/或控制器的物理结构,但是我想尊重@Url.Action("Start", "Build") ,而不必在我的文件中手动编写代码“ build/start ”,更重要的是,保留我的StartControllerUploadController代码,因为我不需要BuildController中的大量代码。

I would switch to attribute routining. 我将切换到属性路由。 You will have to change your ActionLink methods to RouteLink methods but it will work the same way. 您将必须将ActionLink方法更改为RouteLink方法,但其工作方式相同。

Add the attribute 添加属性

[Route("build/start/{id:int}", Name = "WizardStep1")]

for each controller method (changing the route appropriately). 每种控制器方法(适当更改路线)。

To generate a link, use RouteLink: 要生成链接,请使用RouteLink:

@Html.RouteLink("WizardStep1", ...)

Replacing the ... with the additional code you need to set the URL attributes. 用其他代码替换...,您需要设置URL属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM