简体   繁体   English

自定义地图路线在asp.net mvc中不起作用

[英]Custom map route is not working in asp.net mvc

I have an custom mvc route it's not working. 我有一个自定义的mvc路线,它不起作用。 if i define the route before home route then it's working otherwise not. 如果我在回家路线之前定义路线,那么它的工作原理不然。

this code is not working. 这段代码不起作用。

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

       routes.MapRoute(
          "Citysearch",
         "{state}",
         new { controller = "Dashboard", action = "GetDynamicContent" }
       );

when i define the citysearch first then it's working something like this 当我首先定义citysearch然后它就像这样工作

routes.MapRoute(
              "Citysearch",
             "{state}",
             new { controller = "Dashboard", action = "GetDynamicContent" }
           );

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

and another problem is this is the url of city search http://localhost:51381/dynamic-content . 另一个问题是这是城市搜索的http://localhost:51381/dynamic-content dynamic-content this is my state paramter. dynamic-content这是我的状态参数。 It's calling of my Dashboard/GetDynamicContent . 它正在调用我的Dashboard/GetDynamicContent but problem is when application run after login url is http://localhost:51381/Home it is calling always Dashboard/GetDynamicContent how to get rid of this problem please help me. 但问题是当应用程序运行后登录url是http://localhost:51381/Home它总是调用Dashboard/GetDynamicContent如何摆脱这个问题请帮帮我。

Routes are read from top to bottom. 路线从上到下读取。 Therefore, the first route match will be used when routing. 因此,路由时将使用第一个路由匹配。

Try this 试试这个

routes.MapRoute(
    "Home",
    "Home/{action}",
    new { controller = "Home", action = "index" }
);

routes.MapRoute(
    "Citysearch",
    "DynamicContent/{state}",
    new { controller = "Dashboard", action = "GetDynamicContent" }
);

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

You have to use some fix part in your url as shown below DynamicContent/ to solve issue you are facing. 你必须在你的网址中使用一些修复部分,如下所示DynamicContent/来解决你所面临的问题。 And your url should be like http://localhost:51381/DynamicContent/dynamic-content . 你的网址应该像http://localhost:51381/DynamicContent/dynamic-content

routes.MapRoute(
    "Citysearch",
    "DynamicContent/{state}",
    new { controller = "Dashboard", action = "GetDynamicContent" }
);

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

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

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