mieliespoor
mieliespoor

Reputation: 1073

WebAPI AttributeRouting with version number as parameter

I am trying to set up a route to my MVC controller action with the following template:

[Route("app/{appId:length(20)}/validate/{version}")]

The idea is that the resulting URL would then look something like this: (I'm adding a space to make the localhost url try not resolve...)

http:/ /localhost:2642/api/update/v1/app/6A6EE0B355C34DBFB381/validate/1.0.0.0

The problem I have is that this would give me a 404 error.

If I remove the Route attribute and use the built-in MVC routing, it works. The url then is:

http:/ /localhost:2642/api/external/CheckForUpdate?appId=6A6EE0B355C34DBFB381&version=1.0.0.0

This should work since I know Nuget has got versions in their URL's, but I think they use the built-in MVC router.

I also tried to add the :regex() markup in the template for the route so validate the string format, but that didn't work. If I pass in a normal string or a value such as 1_0_0_0 it works. Thing is I don't want to go through the effort to manipulate the version string before sending it to the api and then in the api itself.

Any ideas on what I am doing wrong?

Upvotes: 1

Views: 472

Answers (1)

PMC
PMC

Reputation: 4766

If you make your routing attribute

[Route("app/{appId:length(20)}/validate/{version:regex(^([1-9]\\d+|[0-7])(\\.\\d{1,3}){0,3}$)}")]

and make sure you accept the parameters in the method

public Task<IHttpActionResult> Get(string appId, string version)
{
    //magic
}

then in the web.config, system.webServer > handlers section change the *. to *

<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

to

<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*" verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

and also make sure

<modules runAllManagedModulesForAllRequests="true">

exists in <system.webServer>

Upvotes: 1

Related Questions