简体   繁体   English

使用更改的参数名称在ASP MVC中路由URL

[英]Route Url in ASP MVC with changing parameters name

I'm using Asp.NET mvc 5 我正在使用Asp.NET MVC 5

I have the following controller 我有以下控制器

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

What is the correct way to redirect url /B/ActionB?p1=5&p2=9 to /A/ActionA?param1=p1&param2=p2 ? 将url /B/ActionB?p1=5&p2=9重定向到/A/ActionA?param1=p1&param2=p2的正确方法是什么/A/ActionA?param1=p1&param2=p2

Edit : I tried the following but I'm struggle with converting the parameters 编辑:我尝试了以下方法,但是我在转换参数方面遇到了困难

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

The correct way to redirect is to use RedirectToAction : 重定向的正确方法是使用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") ; 
   } 
}

But do note that calling /B/ActionB?p1=5&p2=9 will reach ActionB and then MVC will respond with a 302 status code telling the browser to fetch the URL /A/ActionA?param1=5&param2=9 . 但是请注意,调用/B/ActionB?p1=5&p2=9将到达ActionB ,然后MVC将以302状态代码响应,告诉浏览器获取URL /A/ActionA?param1=5&param2=9 So, it turns 1 round trip to the server into 2. 因此,它将服务器的往返行程变为2。

From an application design standpoint, it makes more sense to go directly to /A/ActionA?param1=5&param2=9 unless you have some specific reason why you need to change the URL in the user's browser. 从应用程序设计的角度来看,直接进入/A/ActionA?param1=5&param2=9更有意义,除非您/A/ActionA?param1=5&param2=9某些特定原因需要在用户浏览器中更改URL。


If your goal is to divert all traffic from BController.ActionB to AController.ActionA because you are replacing it in your application, you should do a 301 redirect. 如果您的目标是将所有流量从BController.ActionB转移到AController.ActionA因为您正在应用程序中进行替换,则应执行301重定向。 That is best handled by the IIS URL Rewrite Module. 最好由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>

Do note that if the query string parameters are optional or could appear in reverse order, you will need to add additional rules to cover those cases. 请注意,如果查询字符串参数是可选的或可能以相反的顺序出现,则您将需要添加其他规则来覆盖这些情况。

Alternatively, you could use routing in MVC for the 301 redirects. 或者,您可以将MVC中的路由用于301重定向。 There is an example here , although it would need to be modified to extract the query string arguments from the request and pass them to the receiving end using different names. 有一个例子在这里 ,尽管这需要进行修改,以从请求中提取的查询字符串参数,并使用不同的名称将它们传递到接收端。

From what I figured out, the query string can be represented on RouteAttribute using route constraint, hence this query string: 据我了解,查询字符串可以使用路由约束在RouteAttribute上表示,因此该查询字符串为:

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

represented in RouteAttribute argument like below: RouteAttribute参数中表示,如下所示:

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

or with parameters included (not totally sure if this work, hence I preferred the former one): 或包含参数(不确定是否可以正常工作,因此我更喜欢前一个参数):

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

Since you're using MVC 5, the argument should put inside RouteAttribute like this example: 由于您使用的是MVC 5,因此参数应像下面的示例一样放在RouteAttribute

public class AController : Controller {

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

Note that as Ken Egozi said attribute routing with query string parameters (using ? & & ) only supported by Web API controller , as in MVC controller you can bind the query string as action parameter(s) through customized model binder as workaround. 注意, 肯Egozi说属性与查询字符串参数(使用路由?& )只支持的Web API控制器 ,如MVC控制器可以绑定查询字符串通过定制模型绑定的解决方法操作参数(S)。

Similar issue: 类似问题:

Query string not working while using attribute routing 使用属性路由时查询字符串不起作用

Related: 有关:

How do I route a URL with a querystring in ASP.NET MVC? 如何在ASP.NET MVC中路由带有查询字符串的URL?

How to use Querystring Parameters in Asp.Net MVC to Retrieve or Send Data? 如何在Asp.Net MVC中使用Querystring参数检索或发送数据?

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

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