locked
How to migrate the below lines of code into .NET Core RRS feed

  • Question

  • User2056817590 posted
    1. string path1 = ((System.Web.HttpRequestWrapper)((System.Web.HttpContextWrapper)HttpContext).Request).PhysicalApplicationPath;
    2. Response.ClearHeaders();
    3. string url = Request.Url.Host;
    4. bool IsHttps = Request.IsSecureConnection;
    5.  var timeRangeContext = new ValidationContext(this.timeRange, validationContext.ServiceContainer, validationContext.Items);
    6.     string saveFileLocalPath = Server.MapPath(fileRefUrl);
    7.   throw new HttpException(id, "error");
    8. System.Web.HttpContext context = System.Web.HttpContext.Current;
                      string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    Monday, April 12, 2021 4:28 AM

All replies

  • User-474980206 posted

    have you done even a cursory review of the docs: see >>

    1. string path1 = ((System.Web.HttpRequestWrapper)((System.Web.HttpContextWrapper)HttpContext).Request).PhysicalApplicationPath;

    >> no equivalent. No access to IIS settngs. closes is the .net binary location. Assembly.Location  

    2. Response.ClearHeaders();

    >> Request.Headers.Clear()

    3. string url = Request.Url.Host;

    >> Request.Host

    4. bool IsHttps = Request.IsSecureConnection;

    >> bool IsHttp = Request.IsHttps;

    5. var timeRangeContext = new ValidationContext(this.timeRange, validationContext.ServiceContainer, validationContext.Items);

    >> same

    6. string saveFileLocalPath = Server.MapPath(fileRefUrl);

    >> no equivalent. 

    7. throw new HttpException(id, "error");

    >> no direct equivalent. webapi return a status 500 on errors. probably just throw new ApplicationException() or

        return StatusCode(StatusCodes.Status400BadRequest);  

    8. System.Web.HttpContext context = System.Web.HttpContext.Current;

    >> explicitly not implemented. Must refactor code to pass in HttpContext

    9. string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

    >> assuming the header is there (usually filled in by load balancer or proxy):

    string ipAddress = Request.Headers["HTTP_X_FORWARDED_FOR"];

    if unknown:

    string ipAddress = Request.Headers.FirstOrDefault(h => h.Key == "HTTP_X_FORWARDED_FOR")?.Value;

    Monday, April 12, 2021 10:00 PM