【问题标题】:How to hide controller and action name from start url of my mvc project如何从我的 mvc 项目的开始 url 隐藏控制器和操作名称
【发布时间】:2019-05-22 10:04:58
【问题描述】:

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

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 如何从该网址隐藏列表/索引?

【问题讨论】:

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


    【解决方案1】:

    您是否尝试过将默认路线移至 SearchClinicListing 上方,如果可行,请告诉我

    【讨论】:

    • 在上面移动默认路由后,我的应用程序中断,找不到所有其他路由谢谢,
    【解决方案2】:

    为了拥有:

    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

    【讨论】:

    【解决方案3】:

    从 URL 中删除控制器和操作名称不是一个好主意。但是如果你想这样做,你应该在默认路由之前映射新路由:

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

    【讨论】:

      猜你喜欢
      • 2016-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-16
      • 1970-01-01
      • 2020-01-07
      • 1970-01-01
      相关资源
      最近更新 更多