[英]Detect requstURI dynamically + ASP.NET Core
I'm using web.config to set up some rediretion rules using .Net Core. 我正在使用web.config使用.Net Core设置一些重新定义规则。
<rule name="About Page" stopProcessing="true">
<match url="about.aspx" />
<action type="Redirect" url="/Home/About" redirectType="Permanent" />
</rule>
...
So if the user enters www.test.com/about.aspx , it redirects to www.test.com/Home/About page. 因此,如果用户输入www.test.com/about.aspx ,它将重定向到www.test.com/Home/About页面。
My question is how can I pass what the user entered in URL to the controller method? 我的问题是如何将用户在URL中输入的内容传递给控制器方法? For example, www.test.com/about.aspx in this case?
例如,在这种情况下, www.test.com / about.aspx ?
I tried with 我尝试过
var currentURI = new Uri($"{Request.Scheme}://{Request.Host}{Request.Path}{Request.QueryString}");
But that one detects the redirected uri (www.test.com/Home/About), not the initially typed in URL (www.test.com/about.aspx) 但是该用户检测到重定向的uri(www.test.com/Home/About),而不是最初输入的URL(www.test.com/about.aspx)
I'm using the below code in HomeController
, IActionResult
About
method. 我在
HomeController
, IActionResult
About
方法中使用以下代码。
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Head, uri))
HttpRequestMessage
's requestUri
parameter is not dynamic at the moment, so it won't be able to detect what the user entered in URL. HttpRequestMessage
的requestUri
参数目前不是动态的,因此它无法检测到用户在URL中输入的内容。
Help is appreciated. 感谢帮助。
Let's describe the flow of HTTP requests for more clarity: 为了更加清楚,让我们描述HTTP请求的流程:
http://www.test.com/about.aspx
http://www.test.com/about.aspx
http://www.test.com/Home/About
in Location header. http://www.test.com/Home/About
。 http://www.test.com/Home/About
. http://www.test.com/Home/About
。 Second request does not contain any reference to original url http://www.test.com/about.aspx
. 第二个请求不包含对原始URL
http://www.test.com/about.aspx
任何引用。 That's why there is no way for you to get original url on server side . 这就是为什么您无法在服务器端获取原始url的原因。
If your application logic requires having info about initial url, I see two options here: 如果您的应用程序逻辑需要有关初始URL的信息,那么我在这里看到两个选项:
You could add original url as a parameter to redirected url, something like this: http://www.test.com/Home/About?orig_url=http://www.test.com/about.aspx
您可以将原始url作为参数添加到重定向的url中,例如:
http://www.test.com/Home/About?orig_url=http://www.test.com/about.aspx
: http://www.test.com/Home/About?orig_url=http://www.test.com/about.aspx
: http://www.test.com/Home/About?orig_url=http://www.test.com/about.aspx
You have to update redirection rule with new template and also adjust routing in ASP.Net Core application. 您必须使用新模板更新重定向规则,并在ASP.Net Core应用程序中调整路由。
You could use routing instead of redirection. 您可以使用路由而不是重定向。 In this case no 301 redirect will happen, Server will process initial request with the url typed by user, but the same Controller action will be called for both urls.
在这种情况下,不会发生301重定向,服务器将使用用户键入的url处理初始请求,但是两个URL都将调用相同的Controller动作。
Choose the approach that is more suitable for you. 选择更适合您的方法。 Hope this will help.
希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.