locked
How to convert the below piece of code into .NET Core RRS feed

  • Question

  • User2056817590 posted

    public class ExitHttpsIfNotRequiredAttribute : FilterAttribute, IAuthorizationFilter
            {
                public void OnAuthorization(AuthorizationContext filterContext)
                {

                    // redirect to HTTP
                    string url = "http://" + filterContext.HttpContext.Request.Url.Host + filterContext.HttpContext.Request.RawUrl;
                    filterContext.Result = new RedirectResult(url);
                }
            }

    Monday, April 12, 2021 4:22 AM

All replies

  • User1686398519 posted

    Hi kalvakrishna, 

    In ASP.NET Core, the HttpRequest class has no Url and RawUrl properties.

    1. HttpRequest.Host Property
    2. HttpRequest.Path Property
    3. You can use the HttpRequest.Host property and HttpRequest.Path property instead.

    In addition, you need to use ActionFilterAttribute instead of FilterAttribute.

    If you are willing to spend some time reading some of the links below, they can help you understand the problem you are experiencing.

    1. Filters in ASP.NET Core
    2. What's the difference between HttpRequest.Path and HttpRequest.PathBase in ASP.NET Core?
    public class ExitHttpsIfNotRequiredAttribute : ActionFilterAttribute, IAuthorizationFilter
    {
       public void OnAuthorization(AuthorizationFilterContext filterContext)
       {
          //redirect to HTTP
          string url = "http://" + filterContext.HttpContext.Request.Host+ filterContext.HttpContext.Request.Path;
          filterContext.Result = new RedirectResult(url);
       }
    }

    Here is the result. 

    Best Regards,

    YihuiSun

    Tuesday, April 13, 2021 2:14 AM