0

I'm working on mvc4 web api project.

I have created a new controller GroupValuesController and i have following methods

public GroupViewModel Get()
public List<GroupModel> Get(Guid Id)
public List<GroupModel> GetDetails(Guid Id)

First two methods are working fine I'm calling them from a group.cshtml view like following

            $.getJSON(
                "api/groupvalues",
                function (data) {

and

$.getJSON(
                "api/groupvalues/" + id,
                function (data) {

For third controller method public List<GroupModel> GetDetails(Guid Id) i'm executing that from Details.cshtml view like following but it is not working. i'm mismatching some calling ?

 function getGroupDataById(id, ctrl) {
        $.getJSON(
                "api/groupvalues/GetDetails/" + id,
                function (data) {

Is this related with Route?

 public static void Register(HttpConfiguration config)
        {
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
3

2 Answers 2

2

Per the link below, in order to target that action method the way you have, you need to change your routing:

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

http://www.codeproject.com/Articles/624180/Routing-Basics-in-ASP-NET-Web-API

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

3 Comments

True But still call is not going from Detail.cshtml It is going good for 1st two methods those are from Group.cshtml :( Getting getting 404 error for 3rd method when called from Detail.cshtml
i think it is finding method in detail it self see the URL error is 400 http://localhost:14514/Group/Detail/api/groupvalues/GetDetails/af0eaed2-c338-4880-9bf5-40473462e5b3
I did this once. I remember i ended up adding same as the route suggested here, and kept also the default route but as second route.
1

It really doesn't matter what you write after Get, it is going to call the first one with same arguments. Web API don't rely on name rather they rely on HTTP verbs.

1 Comment

Url difference i found is http://localhost:14514/api/groupvalues/Get/2c78a982-4cf4-496b-945b-713c965af6ab --working http://localhost:14514/Group/Detail/api/groupvalues/GetDetails/98760a89-1ab2-42a7-a731-84db5ad30c12 --- not working

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.