【问题标题】:Route Url in ASP MVC with changing parameters name更改参数名称的 ASP MVC 中的路由 Url
【发布时间】:2018-03-24 02:18:08
【问题描述】:

我正在使用 Asp.NET mvc 5

我有以下控制器

public class AController : Controller {
   public ActionResult ActionA(int param1, int param2) { 
         return Content("Whatever") ; 
   } 
}

重定向网址的正确方法是什么 /B/ActionB?p1=5&p2=9/A/ActionA?param1=p1&param2=p2 ?

编辑: 我尝试了以下方法,但我很难转换参数

public class AController : Controller {
   [Route("/B/ActionB")]
   public ActionResult ActionA(int param1, int param2) { 
         return Content("Whatever") ; 
   } 
}

【问题讨论】:

  • 您是否尝试过使用RouteAttributeRouteConfig?如果您使用其中之一或两者,请显示。
  • 是的,但我不知道如何更改参数名称。
  • [Route("/B/ActionB?p1={param1:int}&p2={param2:int}")] - 这够了吗?您可以参考blogs.msdn.microsoft.com/webdev/2013/10/17/…了解更多详情。
  • 它不起作用。它说模板不应该包含?
  • [Route("/B/ActionB/{p1=param1:int}/{p2=param2:int}")] - 哦,好吧,我忘记了关于属性路由的内容...尝试使用此配置。

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


【解决方案1】:

据我所知,查询字符串可以使用路由约束在RouteAttribute 上表示,因此这个查询字符串:

/B/ActionB?p1=5&p2=9

RouteAttribute 参数中表示,如下所示:

/B/ActionB/{param1:int}/{param2:int}

或包含参数(不完全确定这是否可行,因此我更喜欢前一个):

/B/ActionB/{p1=param1:int}/{p2=param2:int}

由于您使用的是 MVC 5,因此应将参数放在 RouteAttribute 中,如下例所示:

public class AController : Controller {

   [Route("/B/ActionB/{param1:int}/{param2:int}")]
   public ActionResult ActionA(int param1, int param2) { 
         return Content("Whatever"); 
   } 
}

请注意,Ken Egozi said 属性路由与查询字符串参数(使用?&)仅由Web API controller 支持,如在 MVC 控制器中,您可以通过自定义将查询字符串绑定为操作参数模型绑定器作为解决方法。

类似问题:

Query string not working while using attribute routing

相关:

How do I route a URL with a querystring in ASP.NET MVC?

How to use Querystring Parameters in Asp.Net MVC to Retrieve or Send Data?

【讨论】:

  • 可以使用带有 MVC 路由的查询字符串(即仅在传递查询字符串参数的情况下才匹配),但最好继承 RouteRouteBase使用Controller 来做到这一点。
【解决方案2】:

重定向的正确方法是使用RedirectToAction:

// URL /B/ActionB?p1=5&p2=9
public class BController : Controller {
    public ActionResult ActionB(int p1, int p2) { 
        return RedirectToAction("ActionA", "A", new { param1 = p1, param2 = p2 });
   } 
}

// URL /A/ActionA?param1=5&param2=9
public class AController : Controller {
    public ActionResult ActionA(int param1, int param2) { 
        return Content("Whatever") ; 
   } 
}

但请注意,调用/B/ActionB?p1=5&p2=9 将到达ActionB,然后MVC 将响应一个302 状态代码,告诉浏览器获取URL /A/ActionA?param1=5&param2=9。因此,它将 1 次往返服务器变成了 2 次。

从应用程序设计的角度来看,直接访问/A/ActionA?param1=5&param2=9 更有意义,除非您有特定原因需要更改用户浏览器中的 URL。


如果您的目标是将所有流量从 BController.ActionB 转移到 AController.ActionA,因为您要在应用程序中替换它,您应该执行 301 重定向。这最好由 IIS URL 重写模块处理。

<?xml version="1.0"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Redirect /B/ActionB?p1=5&p2=9 to /A/ActionA?param1=p1&param2=p2" stopProcessing="true">
                    <match url="^B/ActionB?p1=(\d+)&p2=(\d+)" />
                    <action type="Redirect" url="A/ActionA?param1={R:1}&param2={R:2}" redirectType="Permanent" />
                </rule>         
            </rules>
        </rewrite>
        <httpProtocol>
            <redirectHeaders>
                <!-- This is to ensure that clients don't cache the 301 itself - this is dangerous because the 301 can't change when put in place once it is cached -->
                <add name="Cache-Control" value="no-cache"/>
            </redirectHeaders>
        </httpProtocol>
    </system.webServer>
</configuration>

请注意,如果查询字符串参数是可选的或可能以相反的顺序出现,您将需要添加额外的规则来涵盖这些情况。

或者,您可以使用 MVC 中的路由进行 301 重定向。有一个示例here,尽管需要对其进行修改以从请求中提取查询字符串参数并使用不同的名称将它们传递给接收端。

【讨论】:

  • 抱歉,我无法编辑 BController,所以它不起作用。
  • 如果不能编辑BController,为什么要重定向?您是否在应用程序中将 BController 替换为 AController?
猜你喜欢
  • 2011-11-11
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 2016-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-15
相关资源
最近更新 更多