Invalid non-ASCII or control character in header: 0x0E23 In Asp.Net Core

Dung Do Tien Mar 01 2021 91

I have a project with Asp.Net Core 3.1 and when I redirect the Url to another URL, I got an error :  Invalid non-ASCII or control character in header: 0x0E23

  System.InvalidOperationException: Invalid non-ASCII or control character in header: 0x0E23
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpHeaders.ThrowInvalidHeaderCharacter(Char ch)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpHeaders.ValidateHeaderValueCharacters(StringValues headerValues)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpResponseHeaders.SetValueFast(String key, StringValues value)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpHeaders.Microsoft.AspNetCore.Http.IHeaderDictionary.set_Item(String key, StringValues value)
   at Microsoft.AspNetCore.Http.DefaultHttpResponse.Redirect(String location, Boolean permanent)
   at Microsoft.AspNetCore.Mvc.Infrastructure.RedirectResultExecutor.ExecuteAsync(ActionContext context, RedirectResult result)
   at Microsoft.AspNetCore.Mvc.RedirectResult.ExecuteResultAsync(ActionContext context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultAsync(IActionResult result)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)

You can see the code below: 

 string currentUrl = $"{HttpContext.Request.Path.Value}{HttpContext.Request.QueryString}"; // = /car-honda-766-bangkok/2017-toyota-camry-2-0-g-extremo-รถเก๋ง-4-ประตู-aid20040912
string currentPath = HttpContext.Request.Path.Value;  // = /car-honda-766-bangkok/2017-toyota-camry-2-0-g-extremo-รถเก๋ง-4-ประตู-aid20040912
var urlOrigin = _productSEOService.BuildLinkByTitle(brandName); // =  /car-honda-766/2017-toyota-camry-version2-2-0-g-extremo-รถเก๋ง-4-ประตู-aid20040912
if (urlOrigin != currentUrl)
{
    if (urlOrigin != currentPath)
    {
        return RedirectPermanent(urlOrigin);
    }
}

RedirectPermanent(urlEncode) : This code throw error above. I don't know why, please tell me if you have any suggestions.

Have 1 answer(s) found.
  • T

    Tran Quang Hung Mar 01 2021

    This error throw because your URL has some Unicode characters. You need to encode it before redirect. You can try code below:

     using System.Net;
    -----------------
    string currentUrl = $"{HttpContext.Request.Path.Value}{HttpContext.Request.QueryString}"; // = /car-honda-766-bangkok/2017-toyota-camry-2-0-g-extremo-รถเก๋ง-4-ประตู-aid20040912
    string currentPath = HttpContext.Request.Path.Value;  // = /car-honda-766-bangkok/2017-toyota-camry-2-0-g-extremo-รถเก๋ง-4-ประตู-aid20040912
    var urlOrigin = _productSEOService.BuildLinkByTitle(brandName); // =  /car-honda-766/2017-toyota-camry-version2-2-0-g-extremo-รถเก๋ง-4-ประตู-aid20040912
    var urlEncode = String.Join("/", urlOrigin.Split("/").Select(s => WebUtility.UrlEncode(s)));
    if (urlOrigin != currentUrl)
    {
        if (urlOrigin != currentPath)
        {
            return RedirectPermanent(urlEncode);
        }
    }

    WebUtility.UrlEncode() This method help encode URL for you.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close