1

I Have a Web API endpoint like something below -

[HttpPost]  
[ActionName("ResetPassword")]   
public HttpResponseMessage ResetPassword(string userName, string Template, string SubjectKey,[FromBody] Dictionary<string, string> KeyWords)

As you see, there are 4 parameters to the WebAPI. However, other than the first parameter 'userName', all other parameters are optional. All the parameters are of string type and as such by default nullable.

I have configured the route, using convention based routing (it's a legacy project).

Config.Routes.MapHttpRoute(
             name: "ResetPasswordResetV2",
             routeTemplate: "Email/ResetPassword",
             defaults: new { controller = "Email", action = "ResetPassword", routeValue = true });

I was expecting it to work with either -

http://{base address}/V2/Core/Email/[email protected]&template=&subjectKey=
http://{base address}/V2/Core/Email/[email protected]

Not working. I get a 404. Any tips what I am doing wrong. I have read all kind of SO and doc links and there looks to be too much information to process.

Additionally, what does this 'routeValue = true' means ?

Update : I got it working with the first URL but I would have expected it to work with the second API too. One more Info, my controller has one more Action with similar sets of input parameters but the action name is different (Can that in any way mess it up ?)

3
  • You need to clarify what not working means. do you get an error. is the action hit but the parameters null. what is the desired behavior? Commented Apr 5, 2017 at 0:46
  • I updated my question. Basically I am getting a 404. Commented Apr 5, 2017 at 0:58
  • Then you need to provide a minimal reproducible example that can be used to reproduce the problem. Show more of the target controller. and more of how the routes are configured. those are usually the two main areas that can be causing your issue. Commented Apr 5, 2017 at 1:03

1 Answer 1

2

Try this:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{userName}/{template}/{subjectKey}",
    defaults: new { controller = "Email", action = "ResetPassword", template = UrlParameter.Optional, subjectKey = UrlParameter.Optional 
});

Where, userName is required, but template and subjectKey are optional.

URLs, will look like this (supoussing that template is equal to template1 and subjectKey is equal to 3):

http://{base address}/V2/Core/Email/ResetPassword/[email protected]/template1/3

Or, not having any params, only userName:

http://{base address}/V2/Core/Email/ResetPassword/[email protected]

If it's completely necessary, you can send information as query parameters, but you will have to indicate in Controller.

URL typed:

enter image description here

Params received in Controller:

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

In my case, the client was sending the parameters as part of query parameters. In the example you have shown, the parameters are sent as part of route values. Is there a way to do the same with query parameters ?
I just have made a test and yes, it is possible to send as query parameters, I will update my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.