Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an MVC application, that contains a web application in a simple mvc 4 project. Moreover I have a Windows application, that is a windows execution of the web application in an other windows application project.

The Windows application uses MVC API and makes calls to the MVC Web Application Controller by the name of TfsApiController by a resthelper class.

All the methods are working except the post method. I want to post an object to the tfsapi controller.

The post method of rest helper class:

public void Post(T targetObject)
{
    if (targetObject == null)
    {
        throw new System.ArgumentNullException("targetObject");
    }

    //Get formatting details.
    System.Net.Http.Formatting.MediaTypeFormatter myFormatter = new XmlMediaTypeFormatter();

    myUrlSuffix = "api/tfsapi";

    //Make the call.
    var result = this.MainHttpClient.PostAsync(myUrlSuffix, targetObject, myFormatter).Result.Content.ReadAsAsync<T>().Result;
}

My TfsApiController method:

public void PostPreActivity(PreActivity preActivity)
{
}

And the Routing:

RouteTable.Routes.MapHttpRoute(
name: "PostPreActivity",
routeTemplate: "api/tfsapi/Post",
defaults: new { id = RouteParameter.Optional }
).RouteHandler = new SessionRouteHandler();

I wasted lot of hours, but i do not know what is the problem.

UPDATE:

This is my all routings:

RouteTable.Routes.MapHttpRoute(
         name: "GetWorkItemsByProject",
         routeTemplate: "api/{controller}/{action}/{projectID}",
         defaults: new { controller = "tfsapi", action = "GetWorkItems", projectID = UrlParameter.Optional }
     ).RouteHandler = new SessionRouteHandler();;

RouteTable.Routes.MapHttpRoute(
         name: "ActionWebApiRoute",
         routeTemplate: "api/{controller}/{action}/{id}",
         defaults: new { id = RouteParameter.Optional }
     ).RouteHandler = new SessionRouteHandler();

RouteTable.Routes.MapHttpRoute(
         name: "DefaultApi",
         routeTemplate: "api/{controller}/{id}",
         defaults: new { id = RouteParameter.Optional }
     ).RouteHandler = new SessionRouteHandler();
share|improve this question

1 Answer

You route seems to be incorrect, change it to something like below and try again:

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

by the way, what are you using SessionRouteHandler for?

[Update after the post update above]

You need to change the following code to include the action name completely: myUrlSuffix = "api/tfsapi/PostPreActivity";

I believe this is because based on your updated routings,when a request like 'api/tfsapi' comes, it would be tried to be matched against the 1st route with the name "GetWorkItemsByProject" and since 'action' is not optional here, you are probably seeing 404 not found.

share|improve this answer
Thanks for your help, but this routing is my default routing. I have updated the post. – Concware Feb 22 at 9:10
I have updated my post above based on your routing update. let me know if this helps. – Kiran Challa Feb 22 at 16:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.