简体   繁体   English

MVC 5路由问题-多个路由错误地针对同一视图

[英]MVC 5 Routing Issue - Mutiple Routes Incorrectly Targeting Same View

I have multiple routes configured, but for some reason, despite the rules addressing different Controllers and different Views, different links are routing to the same view. 我配置了多个路由,但是由于某些原因,尽管规则针对的是不同的Controller和不同的View,但不同的链接却路由到同一视图。 Please see below, I have included my RouteConfig file and example links below: 请参见下面,我在下面包含了RouteConfig文件和示例链接:

RouteConfig.cs RouteConfig.cs

using System.Web.Mvc;
using System.Web.Routing;

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

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

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

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

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

            routes.MapRoute(
                    name: "Details",
                    url: "{controller}/{action}/{u}",
                    defaults: new
                    {
                        controller = "Member",
                        action = "Details",
                        u = UrlParameter.Optional
                    }
            );

            routes.MapRoute(
                name: "Article",
                url: "{Home}/{Article}/{id}/{articleName}",
                defaults: new { controller = "Home", action = "Article" }
            );

            routes.MapRoute(
                name: "Item",
                url: "{News}/{Item}/{id}/{itemName}",
                defaults: new { controller = "News", action = "Item" }
            );
        }
    }
}

Links 链接

http://localhost:11508/Home/Article/2/Participate
http://localhost:11508/News/Item/2/Second-Test

As so can see, the links and rules are most certainly unique but for some reason the Item rule is being ignored, it is simply passing Id 2 to the Home/Article view. 可以看到,链接和规则无疑是唯一的,但是由于某些原因,Item规则被忽略,它只是将ID 2传递给Home / Article视图。

You shouldn't include controller / action names in brackets - just pass them as is, so that path can be matched. 您不应该在方括号中包含控制器/动作名称-只需按原样传递它们,以便可以匹配路径。 Your last two routes should look like this: 您的最后两条路线应如下所示:

        routes.MapRoute(
            name: "Article",
            url: "Home/Article/{id}/{articleName}",
            defaults: new { controller = "Home", action = "Article" }
        );

        routes.MapRoute(
            name: "Item",
            url: "News/Item/{id}/{itemName}",
            defaults: new { controller = "News", action = "Item" }
        );

Also, it is good to place such specific routes before any other routes, not after default routes. 此外,它是很好的任何其他路线之前 ,将这种特定的路线, 不缺省路由。

UPDATE 更新

Basically it should be separate question, but it is easier to just answer it here. 基本上,这应该是一个单独的问题,但是在这里简单回答即可。

From comment: 来自评论:

how I can get http://localhost:11508/Member/Details?u=testuser to be routed to http://localhost:11508/Member/Details/testuser instead of a showing parameter. 如何获取http://localhost:11508/Member/Details?u=testuser路由到http://localhost:11508/Member/Details/testuser而不是显示参数。

  1. Create controller action which accepts this parameter, like this one: 创建接受此参数的控制器动作,如下所示:

     public ActionResult Details(string u, ...) { var model = new ... ... return View(model); } 
  2. Register route, which accepts u parameter as URL part, like this one 注册路由,接受u参数作为URL部分,就像这样一个

      routes.MapRoute( name: "MyRoute", url: "Member/Details/{u}", defaults: new { controller = "Member", action = "Details", u = UrlParameter.Optional } ); 

    Here {u} actually declares parameter name, and how it should be used (parsed / rendered) inside URL. 这里的{u}实际上声明了参数名称,以及在URL中应如何使用(解析/呈现)参数名称。

  3. Render link to the URL like this one: 像这样将链接呈现给URL:

     <a href="@Url.Action("Details", "Member", new {u = "testuser"})">linktext</a> 

In all these steps, u is that name of parameter which you will use. 在所有这些步骤中, u是您将使用的参数名称。

The Mapping takes the first matching rule. 映射采用第一个匹配规则。 The "Item"-Route would never be used because the Article-Root will catch all request that could match "Item"-Route. 永远不会使用“项目”路由,因为文章根目录将捕获所有可能匹配“项目”路由的请求。 Check the order of the routes AND delete the {} surrounding news. 检查路线的顺序并删除{}周围新闻。

routes.MapRoute(
            name: "Item",
            url: "News/Item/{id}/{itemName}",
            defaults: new { controller = "News", action = "Item" }
        );

Your problem is in the order in which you are registering your routes. 您的问题出在您的路线注册顺序中。 The rule is that you should register them from the most specific to the least. 规则是您应从最具体到最少注册它们。 In other words, your "default" route(s) should be the very last. 换句话说,您的“默认”路由应该是最后一条。

With how you have it right now, MVC gets a hit on your default route, because your item route matches that, so once it hits on that, it stops looking for other routes and uses it. 借助您现在的方式,MVC会在您的默认路线上受到打击,因为您的item路线与之匹配,因此一旦到达该路线,它就会停止寻找其他路线并使用它。

Move your item route up to the top of your RegisterRoutes method and it should work fine. item路由上移到RegisterRoutes方法的顶部,它应该可以正常工作。

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

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