23

How to pass email as a parameter at the end of URL in ASP.NET MVC web API?

like below:

test.com/api/sales/getcustomerorders/test@test.com

I want to pass the email address as a parameter to the getcustomerorders action.

we can pass using query string. But I want to format the url like above.

Thanks.

User0106
  • 519
  • 1
  • 5
  • 13

3 Answers3

38

In WebApiConfig make the route as "/{email}/". The end "/" will prevent you from getting No HTTP resource was found that matches the request URI ...

Damitha
  • 662
  • 5
  • 7
  • 1
    Easy and simple solution. No configuration changes or anything else needed. Thanks for posting. – Marwan مروان Apr 17 '15 at 17:34
  • 1
    This worked for me. Can you please explain why this works? I don't understand it. – spoof3r Oct 11 '15 at 22:36
  • 1
    The reason it works is it takes that field {email} and it will match that vs the string it is expecting for email, which matches anything following that, including a@b.com and stuffs it into the email var. LOVE this answer btw I spent a few hours trying to google special chars to avoid and everything else (worked until I put an '@' in the URL, then it redirected to login - which was very weird. Thanks! – Traderhut Games Sep 05 '18 at 00:32
9

The problem here is that you have period in the url.

In order to make it work, change you web config to add following two configurations:

<configuration>
  <system.web>
    <httpRuntime relaxedUrlToFileSystemMapping="true" />
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

Haacked has a blog about relexUrlToFileSystemMapping http://haacked.com/archive/2010/04/29/allowing-reserved-filenames-in-URLs.aspx

And I have a bug about it in IIS express 8 and need the runAllManagedModulesForAllRequests: https://aspnetwebstack.codeplex.com/workitem/226

Hongye Sun
  • 3,868
  • 1
  • 25
  • 18
  • It works but if you refer to [this link](http://stackoverflow.com/questions/11048863/modules-runallmanagedmodulesforallrequests-true-meaning) it will slow your app because you load all module for all requests.. – davidlebr1 Aug 17 '15 at 20:09
  • just runAllManagedModulesForAllRequests is needed to allow an email address since a period is used in the URL – user2320464 Oct 08 '15 at 15:01
7

Scott Hanselman covers this in this blog post. As an alternative you could pass the email as a query string parameter and not as part of the path since certain characters are not allowed.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928