开发者

ASP.NET MVC (2) NormalizeUrl Action Filter Attribute (C#)

开发者 https://www.devze.com 2023-01-10 18:46 出处:网络
Can someone verify the integrity of this chunk of code? I want to decorate the controllers with it so that it will normalize the url to either https and/or www. This is my second rewrite of the code b

Can someone verify the integrity of this chunk of code? I want to decorate the controllers with it so that it will normalize the url to either https and/or www. This is my second rewrite of the code because the first one tossed itself 开发者_运维问答into an infinite redirect, but somehow I still think it's not quite there...

public class NormalizeUrl : ActionFilterAttribute {
    private bool ForceHttps = false;
    private bool ForceWww = false;

    public NormalizeUrl() {
    }

    public NormalizeUrl(
        bool ForceHttps,
        bool ForceWww) {
        this.ForceHttps = ForceHttps;
        this.ForceWww = ForceWww;
    }

    public override void OnActionExecuting(
        ActionExecutingContext Context) {
        HttpRequestBase Request = Context.HttpContext.Request;
        HttpResponseBase Response = Context.HttpContext.Response;

        if (!Request.IsLocal) {
            Uri Uri;

            if (!Request.IsSecureConnection && ForceHttps) {
                if (ForceWww) {
                    Uri = new Uri(Uri.UriSchemeHttps + "://www." + Request.Url.Host.Replace("www.", string.Empty) + "/" + Request.Url.AbsolutePath);
                } else {
                    Uri = new Uri(Uri.UriSchemeHttps + "://" + Request.Url.Host.Replace("www.", string.Empty) + "/" + Request.Url.AbsolutePath);
                };
            } else if (!ForceHttps) {
                if (ForceWww) {
                    Uri = new Uri(Uri.UriSchemeHttp + "://www." + Request.Url.Host.Replace("www.", string.Empty) + "/" + Request.Url.AbsolutePath);
                } else {
                    Uri = Request.Url;
                };
            } else {
                Uri = Request.Url;
            };

            Response.RedirectPermanent(Uri.AbsoluteUri, true);
        };
    }
}

Thanks in advance!


Anyway, after breaking my site a couple of times, I came up with this stable code:

public class NormalizeUrl : ActionFilterAttribute {
    private bool ForceHttps = false;
    private bool ForceWww = false;

    public NormalizeUrl(
        bool ForceHttps,
        bool ForceWww) {
        this.ForceHttps = ForceHttps;
        this.ForceWww = ForceWww;
    }

    public override void OnActionExecuting(
        ActionExecutingContext Context) {
        HttpRequestBase Request = Context.HttpContext.Request;
        HttpResponseBase Response = Context.HttpContext.Response;

        if (!Request.IsLocal) {
            if (!Request.IsSecureConnection && this.ForceHttps) {   //  http://domain.com OR http://www.domain.com
                if (this.ForceWww && !Request.Url.Host.Contains("www.")) {  //  http://domain.com
                    Response.RedirectPermanent(new Uri(Uri.UriSchemeHttps + "://www." + Request.Url.Host + Request.Url.AbsolutePath).AbsoluteUri, true);
                } else {    //  http://www.domain.com
                    Response.RedirectPermanent(new Uri(Uri.UriSchemeHttps + "://" + Request.Url.Host + Request.Url.AbsolutePath).AbsoluteUri, true);
                };
            } else {
                if (this.ForceWww && !Request.Url.Host.Contains("www.")) {  //  http://domain.com OR https://domain.com
                    Response.RedirectPermanent(new Uri(Request.Url.Scheme + "://www." + Request.Url.Host + Request.Url.AbsolutePath).AbsoluteUri, true);
                };
            };
        };
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消