Sam*_*ron 16 c# iis web-config azure asp.net-mvc-2
我有一个名为azure的天蓝色网站:
http://myapp.cloudapp.net当然这个URL有点难看,所以我设置了一个指向http://www.myapp.com天蓝色网址的CNAME.
一切都很好,直到这里,但有一个障碍.
http://myapp.cloudapp.net 已泄露,现在由谷歌索引,并生活在其他网站上.
我想将myapp.cloudapp.net的任何请求永久重定向到www.myapp.com的新家
我的网站是用MVC.Net 2.0编写的,因为这是一个天蓝色的应用程序,没有用于访问IIS的UI,所有内容都需要在应用程序代码或web.config中完成.
什么是设置永久重定向的简洁方法,如果它放在web.config或全局控制器中?
use*_*559 18
您可能希望改为使用IIS重写模块(看起来"更干净").这是一篇博客文章,展示了如何执行此操作:http://weblogs.asp.net/owscott/archive/2009/11/30/iis-url-rewrite-redirect-multiple-domain-names-to-one.aspx.(您只需将相关标记放在web.config中.)
您可以使用的示例规则是:
<rule name="cloudexchange" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="cloudexchange.cloudapp.net" />
</conditions>
<action type="Redirect" url="http://odata.stackexchange.com/{R:0}" />
</rule>
Run Code Online (Sandbox Code Playgroud)
这就是我做的:
我们有一个我们用于所有控制器的基本控制器类,我们现在覆盖:
protected override void OnActionExecuted(ActionExecutedContext filterContext) {
var host = filterContext.HttpContext.Request.Headers["Host"];
if (host != null && host.StartsWith("cloudexchange.cloudapp.net")) {
filterContext.Result = new RedirectPermanentResult("http://odata.stackexchange.com" + filterContext.HttpContext.Request.RawUrl);
} else
{
base.OnActionExecuted(filterContext);
}
}
Run Code Online (Sandbox Code Playgroud)
并添加了以下类:
namespace StackExchange.DataExplorer.Helpers
{
public class RedirectPermanentResult : ActionResult {
public RedirectPermanentResult(string url) {
if (String.IsNullOrEmpty(url)) {
throw new ArgumentException("url should not be empty");
}
Url = url;
}
public string Url {
get;
private set;
}
public override void ExecuteResult(ControllerContext context) {
if (context == null) {
throw new ArgumentNullException("context");
}
if (context.IsChildAction) {
throw new InvalidOperationException("You can not redirect in child actions");
}
string destinationUrl = UrlHelper.GenerateContentUrl(Url, context.HttpContext);
context.Controller.TempData.Keep();
context.HttpContext.Response.RedirectPermanent(destinationUrl, false /* endResponse */);
}
}
}
Run Code Online (Sandbox Code Playgroud)
原因是我想要一个永久重定向(而不是临时重定向),以便搜索引擎纠正所有不良链接.