Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bad Request - Invalid URL - HTTP Error 400. The request URL is invalid

I have a routing as per below

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

Now for the ID part, I'm combining many sets of data delimited by "-" character and the encrypt it with AES (RijndaelManaged) encryption. And the I UrlEncode it and send to someone email as per below...

http://localhost:7777/Product/Invite/%e3%b7%92%e4%97%ad%eb%b6%b3%e3%b7%90%eb%b2%8c%e2%aa%ad%e7%86%87%e9%b6%9e%ec%ae%aa%ec%a7%a2%ec%9a%a0%ec%ba%be%e2%89%bc%e5%ba%aa%ee%be%a8%ee%9f%a8%ea%aa%b6%e3%87%83%e3%8c%b4%eb%99%89%e8%8f%bb%e4%b0%ab%ef%bf%bd%e7%a0%99

but upon clicking it I'm getting error.

Bad Request - Invalid URL

HTTP Error 400. The request URL is invalid.

Below is the encryption and encoding process

 string data1 = email.EmailID + "  - /" + events.EventID + "-" + DateTime.Now.ToString("yyyyMMddhhmm") + "-" + events.MemberID;

 var encoding = new UTF8Encoding(false, true);
 byte[] cypherBytes = advEncryProvider.Encrypt(encoding.GetBytes(data1));
 string dataEncodedString = HttpUtility.UrlEncode(GetString(cypherBytes));

Tried editing web.config as How do I enable special characters in MVC routing?

but still no luck

 <system.web>
<httpRuntime targetFramework="4.5" requestPathInvalidCharacters="" requestValidationMode="2.0"/>
<compilation targetFramework="4.5" debug="true"/>
<pages validateRequest="false">
  <namespaces>
    <add namespace="System.Web.Helpers"/>
    <add namespace="System.Web.Mvc"/>
    <add namespace="System.Web.Mvc.Ajax"/>
    <add namespace="System.Web.Mvc.Html"/>
    <add namespace="System.Web.Optimization"/>
    <add namespace="System.Web.Routing"/>
    <add namespace="System.Web.WebPages"/>
  </namespaces>
</pages>

** Update **

I managed to solve this by doing base64 encoding as suggested by Callem Pittard... :)

An efficient way to Base64 encode a byte array?

like image 918
user3663854 Avatar asked Oct 16 '14 07:10

user3663854


2 Answers

The URL character max length by default is 260 as defined by the HTTP.SYS registry. I think base64 encoding your URL just shortened it below this limit. You can override this limit if you need to by added a new value to the registry.

See this article for more information: https://support.microsoft.com/en-us/kb/820129

like image 142
Rafiki Avatar answered Sep 28 '22 15:09

Rafiki


I had the same error. It turned out that there was an invisible bad character in the URL, when I had pasted the URL from Teams into Postman. It sounds like a silly error, but took me a few hours to figure out. So if you get this error, make sure you don't paste the info, but type it in, also make sure there are no hidden space characters, and see if it gets rid of the problem.

Also check for hidden trailing spaces if you are getting the error "The resource cannot be found" in Postman.

like image 32
live-love Avatar answered Sep 28 '22 15:09

live-love