如何从我的MVC项目的起始URL隐藏控制器和动作名称

时间:2018-12-22 04:56:41

标签: c# asp.net-mvc asp.net-mvc-routing

这是我的路由配置文件:-

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
RouteValueDictionary keyValues = new RouteValueDictionary();
PDCL.API.API _api = new PDCL.API.API();
string cities = _api.GetCities();
string Specialities = _api.GetRouteSpecialization();

routes.MapRoute("SearchClinicListing", "{cityname}/{clinic}/{doctortype}/{clinicname}", new { controller = "Listing", action = "ClinicSearch" }, new RouteValueDictionary { { "cityname", cities } });

routes.MapRoute("AreaListing", "{cityname}/{clinicname}/{areaname}", new { controller = "Listing", action = "AreaSearch" }, new RouteValueDictionary { { "cityname", cities }, { "clinicname", Specialities } });

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Listing", action = "Index", id = UrlParameter.Optional }
);

当我运行项目时,默认URL为 http://localhost:2315/listing/index 如何隐藏该网址的列表/索引?

3 个答案:

答案 0 :(得分:0)

您是否尝试过将默认路线移到SearchClinicListing上方,让我知道它是否有效

答案 1 :(得分:0)

要拥有:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Listing", action = "Index", id = UrlParameter.Optional }

你为什么不拥有它?

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

将您的控制器从ListingController重命名为HomeController,因为您希望它作为您的开始控制器,并且比您午餐时做的项目还要多:http://localhost:2315

答案 2 :(得分:0)

从URL中删除控制器和动作名称不是一个好主意。但是,如果要执行此操作,则应在默认路由之前映射新路由:

routes.MapRoute(
name: "MyCustomRoute",
url: "{id}",
defaults: new { controller = "Listing", action = "Index", id = UrlParameter.Optional }
);