1

I am using web api 1 mvc 4

I have following code

public void save(int id , string name, string code = "")
{

}

I have following mapping in webapi.config.

config.Routes.MapHttpRoute(
                 name: "saveproduct",
                 routeTemplate: "api/product/save/{id}/{name}/{code}.{ext}",
                 defaults: new
                 {
                     controller = "product",
                     code = RouteParameter.Optional,
                     action = "save"
                 });

Now If I call

localhost://api/product/save/1/book/p23.json

It works

But

localhost://api/product/save/1/book/.json

It does not work

The reason I found is because optional parameter "code" is in between.

How can I make it work.. As I need extension must at the end with optional parameter like "code".

I found url , but could not got anything related.

Thanks

Community
  • 1
  • 1
Md. Parvez Alam
  • 4,326
  • 5
  • 48
  • 108

1 Answers1

0

Short: you can't.

Long: you can't have an optional parameter in the middle of route because the system is not able to understand if the ".json" is the value of your {code} or the value of your {ext} parameter.

If you want to use this kind of approach you need to force the code parameter always.

BTW, a route like localhost://api/product/save/1/book/.json has no sense... .json it's like a "file" extension, but there is no file name here. You shouldn't allow this kind of call.

Luca Ghersi
  • 3,261
  • 18
  • 32