指正在MVC问题的URL路径选择 [英] Correct me on URL routing in MVC issue

查看:129
本文介绍了指正在MVC问题的URL路径选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是有关我的问题,我问在这个环节<一个href=\"http://stackoverflow.com/questions/28153006/correct-me-on-url-routing-in-mvc/28153091#28153091\">correct我在MVC URL路由

This is related my question which i asked in this link correct me on url routing in mvc

现在我想出了另外一个问题,所以我想我会问它作为新的问题。

Now i came with another problem, so i thought i will ask it as new question.

在我的Global.asax文件现在我有以下途径

Now i have following routes in my global.asax file

routes.MapRoute(
           "Custom", // Route name
           "{action}/{id}", // URL with parameters
           new { controller = "Authentication", action = "BigClientLogin", id = UrlParameter.Optional } // Parameter defaults
       );

routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Authentication", action = "BigClientLogin", id = UrlParameter.Optional } // Parameter defaults
        );

现在发生的事情是,当我运行我的解决方案,我得到的URL为的http://本地主机:65423 /登录这就是我需要为我的登录页面这是确定的。但是,当我登录作为用户我得到的的资源不能找到错误。

Now what happens is when i run my solution the URL i am getting is http://localhost:65423/Login this is what i need for my Login Page that is OK. But when i login in as user i am getting "The resource cannot be found" error.

当我检查我可以看到我的网址,现在改为的http://本地主机:65423 /管理员/控制台

when i checked it i can see that my URL is now changed to "http://localhost:65423/Admin/Dashboard"

所以我觉得这导致了问题。所以,这看起来与我的Global.asax路由问题。

So i think this causing the issue. So this looks the problem related to my global.asax routing.

谁能帮我找出我做错了什么。

Can anyone help me to find out what i did wrong.

推荐答案

您有2条路线与完全可选的段。的问题是,没有办法的路由架构来区分它们。

You have 2 routes with completely optional segments. The issue is that there is no way for the routing framework to differentiate between them.

你可以把它与您现有的工作路线的唯一方法是通过名字明确指定使用它们 @ Html.RouteLink @ Html.RouteUrl )。

The only way you can make it work with your existing routes is to specify them explicitly by name (such as when using @Html.RouteLink or @Html.RouteUrl).

@Html.RouteLink("Custom Link 1", "Custom", new { action = "BigClientLogin" })
@Html.RouteLink("Custom Link 2", "Custom", new { action = "Action2" })
@Html.RouteLink("Home Page", "Default", new { controller = "Home", action = "Index" })
@Html.RouteLink("About", "Default", new { controller = "Home", action = "About" })

做这样的说法会发挥作用,但不正常。通常情况下,只存在一个路径与所有默认值的控制器,操作配置,和id,其余具有一些显式声明区段和/或约束(段是preferable)

Doing it that way will function, but is not normal. Typically, there is only one route configured with all defaults for the controller, action, and id and the rest have some explicitly declared segments and/or constraints (segments being preferable).

routes.MapRoute(
   "Custom", // Route name
   "Custom/{action}/{id}", // URL with parameters
   new { controller = "Authentication", action = "BigClientLogin", id = UrlParameter.Optional } // Parameter defaults
);

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Authentication", action = "BigClientLogin", id = UrlParameter.Optional } // Parameter defaults
);

当网址开头是第一条路线现在将只匹配 /自定义/ 。如果没有自定义启动,它将匹配缺省路由。

The first route will now only match when the URL starts with /Custom/. If it does not start with custom, it will match the default route.

关键是要确保路由在正确的顺序排列,他们只匹配在特定情况下的URL,让他们传递到下一个路径列表,如果情况是不正确的。

The trick is to ensure that the routes are listed in the right order and that they only match the URL in specific cases, letting them pass on to the next route in the list if the case is not correct.

这篇关于指正在MVC问题的URL路径选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆