如何为ASP.NET MVC站点的主页设置路由?

时间:2022-06-22 03:31:14

I'm working with an ASP.NET MVC site which will use a CMS controller for all pages of the site except for the home page. Here's the idea:

我正在使用ASP.NET MVC站点,该站点将使用CMS控制器来访问除主页之外的站点的所有页面。这是个主意:

Home controller:

家庭控制器:

  • www.site.com
  • www.site.com
  • www.site.com/default.aspx
  • www.site.com/default.aspx

CMS Controller:

CMS控制器:

  • www.site.com/about
  • www.site.com/about
  • www.site.com/agenda/schedule
  • www.site.com/agenda/schedule
  • www.site.com/monkey/eats/spaghetti
  • www.site.com/monkey/eats/spaghetti
  • (pretty much anything else)
  • (几乎其他任何东西)

This page lists some options on how to set up a default page routing:

此页面列出了有关如何设置默认页面路由的一些选项:

  1. Leave Default.aspx unrouted and unredirected as the entry point to your application - with static links that take your users into the MVC portion of the app (or other static content).
  2. 保留Default.aspx unrouted和unredirected作为应用程序的入口点 - 使用静态链接将用户带入应用程序的MVC部分(或其他静态内容)。
  3. Redirect Default.aspx in the code behind, either using the Page_Load event handler code, or use Response.Redirect("~/home") to send them to the Home controller (although this is a round-trip redirect).
  4. 在后面的代码中重定向Default.aspx,使用Page_Load事件处理程序代码,或使用Response.Redirect(“〜/ home”)将它们发送到Home控制器(尽管这是一个往返重定向)。
  5. Rename or delete Default.aspx. Despite the warning in the markup that says that default.aspx is required to ensure that ASP.NET MVC is activated by IIS when a user makes a "/" request... it's not actually needed in either the VS dev server, or IIS7. The default request will remain an application root request "/" and will be caught by the default route and sent to the home controller.
  6. 重命名或删除Default.aspx。尽管标记中的警告声明需要default.aspx来确保当用户发出“/”请求时IIS已激活ASP.NET MVC ......在VS dev服务器或IIS7中实际上并不需要它。默认请求将保留为应用程序根请求“/”,并将被默认路由捕获并发送到主控制器。

I guess one other option is to just use one controller with some logic that detects the home page case, but that seems to be fighting the concept.

我想另一个选择是只使用一个控制器和一些检测主页情况的逻辑,但这似乎与这个概念作斗争。

How do you recommend setting up a specific route for the site home page?

您如何建议为站点主页设置特定路由?

3 个解决方案

#1


14  

www.site.com can be handled by an root map route

www.site.com可以通过根映射路径处理

routes.MapRoute(
    "Root",
    "",
    new { controller = "Home", action = "Index", id = "" }
);

Put the following in page load of Default.aspx

将以下内容放在Default.aspx的页面加载中

HttpContext.Current.RewritePath(Request.ApplicationPath, false);
IHttpHandler httpHandler = new MvcHttpHandler();
httpHandler.ProcessRequest(HttpContext.Current);

This rewrites the request to root and handled by the map route above.

这会将请求重写为root并由上面的地图路由处理。

BTW, you can actually find the code from the MVC template project.

顺便说一句,您实际上可以从MVC模板项目中找到代码。

#2


3  

If hosting on IIS7 integrated mode, I suggest just getting rid of default.aspx. As I understand it, it's only necessary for activation on IIS6 and IIS7 classic mode.

如果在IIS7集成模式下托管,我建议摆脱default.aspx。据我了解,它只需要在IIS6和IIS7经典模式下激活。

#3


0  

I think option #1 is easiest. I probably will stick to it until finding a strong reason to move or finding the alternative. The default template uses this approach.

我认为选项#1最简单。我可能会坚持下去,直到找到移动或寻找替代方案的有力理由。默认模板使用此方法。

[UPDATE] Canton beat me to it

[更新]广州打败了我

#1


14  

www.site.com can be handled by an root map route

www.site.com可以通过根映射路径处理

routes.MapRoute(
    "Root",
    "",
    new { controller = "Home", action = "Index", id = "" }
);

Put the following in page load of Default.aspx

将以下内容放在Default.aspx的页面加载中

HttpContext.Current.RewritePath(Request.ApplicationPath, false);
IHttpHandler httpHandler = new MvcHttpHandler();
httpHandler.ProcessRequest(HttpContext.Current);

This rewrites the request to root and handled by the map route above.

这会将请求重写为root并由上面的地图路由处理。

BTW, you can actually find the code from the MVC template project.

顺便说一句,您实际上可以从MVC模板项目中找到代码。

#2


3  

If hosting on IIS7 integrated mode, I suggest just getting rid of default.aspx. As I understand it, it's only necessary for activation on IIS6 and IIS7 classic mode.

如果在IIS7集成模式下托管,我建议摆脱default.aspx。据我了解,它只需要在IIS6和IIS7经典模式下激活。

#3


0  

I think option #1 is easiest. I probably will stick to it until finding a strong reason to move or finding the alternative. The default template uses this approach.

我认为选项#1最简单。我可能会坚持下去,直到找到移动或寻找替代方案的有力理由。默认模板使用此方法。

[UPDATE] Canton beat me to it

[更新]广州打败了我