HOW TO ROUTE THE CONTROLLER TO A DIFFERENT NAME

Pravesh Singh

Total Post:412

Points:2888
Posted by  Pravesh Singh
 961  View(s)
Ratings:
Rate this:

Have a really basic thing to do - I need to expose a post API such as mobile/api/pswdrec which I wanna bind to my PasswordController to avoid that dull name.

 

It's a webapi with areas and I'm trying both Attribute Routing and MapHttpRoute. neither works so far.

 

public class PasswordController : MobileApiControllerBase

{

    //[Route("api/mobile/pswdrec/")]

    public RestResponseMessage Post(UserCredentialsModel credentials)

    {

        return RestResponseMessage.OK();

    }

}

Checked the client side and the requests are proper json requests. It's starting to work when I simply change the name of the controller to PswdrecController.

 

Is there any other way to "rename" the controller?

  1. Sumit Kesarwani

    Post:378

    Points:2694
    Re: how to route the controller to a different name

    Hi Pravesh, 


    try this:

    config.Routes.MapHttpRoute(

                    name: "MyCustomRouteApi",

                    routeTemplate: "api/{controller}/{id}",

                    defaults: new { controller = "mobile", action = "pswdrec", id = RouteParameter.Optional}               

                );

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!