【问题标题】:Differentiate MVC routing for {Controller}/parameter to {Controller}/{action}?param=myvalue将 {Controller}/参数的 MVC 路由区分为 {Controller}/{action}?param=myvalue
【发布时间】:2017-05-25 16:53:04
【问题描述】:

我必须能够处理这样的路线: MyController/ElementType 为此,我创建了这样的自定义路线:

  context.MapRoute(
                  "NameOfTheRoute",
                  "MyPath/{controller}/{elementType}",
                  new { controller = "Elements", action = "Create" }
                  );

而且效果很好,问题是当我有类似的路线时 /MyPath/Elements/GetElementType?elementType=fire88

GetElementType 是一个不同的动作,但是由于我之前声明的自定义路由,它转到了Create 动作,我怎样才能知道它们是不同动作的路由?

【问题讨论】:

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


    【解决方案1】:

    之所以走这条路线,是因为您没有定义route 来处理action 所以MyPath/{controller}/{elementType} 意味着在controller 的名称之后,所有内容都将被视为{elementType} 所以 你必须创建另一个处理action的路由

    routes.MapRoute(
        "MyPathRouteWithAction",
        "MyPath/{controller}/{action}/{elementType}",
        new {controller = "Elements", action = "Create"}
    );
    
    routes.MapRoute(
        "NameOfTheRoute",
        "MyPath/{controller}/{elementType}",
        new {controller = "Elements", action = "Create"}
    );
    

    第一个 custom route 将像 /MyPath/Elements/GetElementType/fire88 一样处理 routs

    【讨论】:

    • 在我的情况下,网址不是/MyPath/Elements/GetElementType/fire88,就像/MyPath/Elements/GetElementType?elementType=fire88。我目前所做的是创建一个执行如下操作的链接:` link 1` 并修改default 路由,刚刚更改idelementType 现在它正在工作,但我不确定这是最好的方法......
    • @AlexGH 你应该使用<a href="@Url.Action("Elements", "GetElementType", new { elementType= "fire88" })">link 1</a>`
    • 我首先尝试了这种方式:link 1但我得到的网址是这样的:/MyPath/Elements/GetElementType?elementType=fire88 我可能没有正确更新浏览器,因为我现在又试了一次,并且正在这样工作,谢谢
    猜你喜欢
    • 2012-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多