简体   繁体   English

动态检测requstURI + ASP.NET Core

[英]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. 我在HomeControllerIActionResult 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. HttpRequestMessagerequestUri参数目前不是动态的,因此它无法检测到用户在URL中输入的内容。

Help is appreciated. 感谢帮助。

Let's describe the flow of HTTP requests for more clarity: 为了更加清楚,让我们描述HTTP请求的流程:

  1. HTTP Client sends initial request to http://www.test.com/about.aspx HTTP客户端将初始请求发送到http://www.test.com/about.aspx
  2. Your redirect rule hits and server responds with 301 code and redirect URL http://www.test.com/Home/About in Location header. 您的重定向规则命中,服务器响应301代码,并在Location标头中重定向URL http://www.test.com/Home/About
  3. HTTP Client receives this response and realizes that it should resend request with new URL HTTP客户端收到此响应,并意识到它应使用新的URL重新发送请求
  4. HTTP Client sends second request to new URL http://www.test.com/Home/About . HTTP客户端将第二个请求发送到新URL 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的信息,那么我在这里看到两个选项:

  1. 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应用程序中调整路由。

  2. 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.

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