1

I have my WebApiController with name AdminDashBoard.

 public class AdminDashBoardController : ApiController
 {
        [System.Web.Http.AcceptVerbs("GET")]       
        public HttpResponseMessage GetCaseHistory(string CaseRefId, string token)
        {
           **Implementation**
        }
 }

I am able to access API using
Localhost/api/AdminDashBoard/GetCaseHistory?CaseRefId=CTcs004&token=eygk

But i want to access this by custom name such as
Localhost/api/Cases/GetCaseHistory?CaseRefId=CTcs004&token=eygk

I have defined customroutes in my WebApiConfig but it is not working.

config.Routes.MapHttpRoute("CaseHistory", "api/cases/{action}/{CaseRefId}/{token}", defaults: new { controller = "AdminDashBoard", action = "GetCaseHistory", CaseRefId = RouteParameter.Optional, token = RouteParameter.Optional });

1 Answer 1

2

the routing you configured will resolve to:

controller => AdminDashBoard
action => GetCaseHistory

But please note that since you've placed /{action} in rout template string, it will affect detection of routing to expect an "action" specified in request URL.

could you try this:

config.Routes.MapHttpRoute("CaseHistory",
            "api/Cases/GetCaseHistory", 
            defaults: 
              new { controller = "AdminDashBoard", 
                    action = "GetCaseHistory" });
config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

remember to place your route before default route as default route will capture the request before the customized one.

In addition, you don't need to define parameters "CaseRefId" and "token" in query string into routing, it should be resolved if you have defined correct type in controller method.

Hope this helps.

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

Comments

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.