[英]How to rewrite to an url
In IIS url rewriting it's able to do rewrite to another url doing the following: 在IIS网址重写中,可以执行以下操作以将其重写为另一个网址:
<rule name="Blog" stopProcessing="true">
<match url="(?:^blog|weblog)(?:/)?(.*)" />
<action type="Rewrite" url="http://blog.domain.com/{R:1}" appendQueryString="true" logRewrittenUrl="true" />
</rule>
So the url when the url domain.com/blog is called the call is rewriten to blog.domain.com (but the url in the browser stays the same). 因此,当调用domain.com/blog URL时,URL会被重写为blog.domain.com(但浏览器中的URL保持不变)。
How would I accomplish the same within C# code? 我如何在C#代码中完成相同的工作?
I have the HttpContext, but then I can only redirect. 我有HttpContext,但随后只能重定向。 There is an RewritePath method but giving an url as parameter results in a "not a valid virtual path" exception.
有一个RewritePath方法,但是将url作为参数给出会导致“无效的虚拟路径”异常。
I am not sure whether its called rewriting however the act is like rewriting an url. 我不确定它是否称为重写,但是这种行为就像重写URL。 simple, just create a webrequest, get the response of the webrequest and return the result as the html to the webpage.
简单,只需创建一个webrequest,获取该webrequest的响应,然后将结果作为html返回到网页。
If you are using ASP.Net MVC this might be helpful, 如果您使用的是ASP.Net MVC,则可能会有所帮助,
public virtual ActionResult RealUrl()
{
var request = WebRequest.Create("http://rewriteUrl.com/blahblah");
// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;
// Get the response.
var response = (HttpWebResponse)request.GetResponse();
// Display the status.
// Get the stream containing content returned by the server.
var dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
if (dataStream == null) return null;
var reader = new StreamReader(dataStream);
// Read the content.
var responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
return Content(responseFromServer);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.