MVC - Rewrite URL to display domain only

强颜欢笑 提交于 2019-12-11 04:19:53

问题


I am using MVC 1.0 in .NET 3.5 Framework. I have a client who would like the URL to be displayed as www.example.com instead of www.example.com/{controller}/{action}/{id}.

  1. The server that the site is hosted on is utilizing IIS 6.
  2. I do not have direct access to this server.

With that in mind, can this be done?

Someone suggested using ISAPI_REWRITE. I've found a few examples but none that actually explain correctly what needs to be done.

Alternatively, if this cannot be done throughout the site. Can it just be done on the Home page?

Below is my routing at the moment:

public static void RegisterRoutes(RouteCollection routes)
{
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
     // Catch all route
     routes.MapRoute(
     "Default", // Name
     "{lang}/{controller}" + System.Configuration.ConfigurationManager.AppSettings["extension"] + "/{action}/{*values}",  // URL - ["extension"] being .aspx for IIS 6
     new { lang = "EN", controller = "Content", action = "Index" } // Defaults);
 }

Thanks in advance.


回答1:


You should write

public static void RegisterRoutes(RouteCollection routes)
{
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

  routes.MapRoute(
         "HomeIndex", // Name
         "",
         new { lang = "EN", controller = "Home", action = "Index" } // Defaults);


     // Catch all route
     routes.MapRoute(
     "Default", // Name
     "{lang}/{controller}" + System.Configuration.ConfigurationManager.AppSettings["extension"] + "/{action}/{*values}",  // URL - ["extension"] being .aspx for IIS 6
     new { lang = "EN", controller = "Content", action = "Index" } // Defaults);
 }


来源:https://stackoverflow.com/questions/12495577/mvc-rewrite-url-to-display-domain-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!