0

What is the equivalent in C#(from the controller(MVC)) for POSTing value into a Web API method using a standard jQuery $.post,

I have it working using a standard jQuery $.post, like this:

  $.post('/api/reports', { AgentId: AgentId, ReportName: ReportName, Params: Params, StatsEmailedId: StatsEmailedId, HeaderName: HeaderName,
        RankBy: RankBy, SoldCountsInclude: SoldCountsInclude, SortBy: SortBy, IncludeCo: IncludeCo, DisplayTop: DisplayTop, UserOffice: UserOffice
    },
function (data) { ....
      }
});
2
  • I'm not sure I understand - do you mean you want to call one of your own WebAPI methods from your controller? Commented Oct 29, 2013 at 22:32
  • Thats right. The Webapi is in a separate project from the project I want to call it from. Commented Oct 29, 2013 at 22:35

3 Answers 3

1

If you want to call an WebApi method in a different project/somewhere else from your controller, I would use the WebClient. You can use this to make any web call from C#. Check out specifically its UploadData method:

http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx

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

Comments

1

I'm not 100% certain I understand fully the question. If you are meaning how to post to a controller from jquery (rather than api), then it's a simple change along the lines of:

  $.post('@Url.Action("actioname","controller")', { AgentId: AgentId, ReportName: ReportName, Params: Params, StatsEmailedId: StatsEmailedId, HeaderName: HeaderName,
        RankBy: RankBy, SoldCountsInclude: SoldCountsInclude, SortBy: SortBy, IncludeCo: IncludeCo, DisplayTop: DisplayTop, UserOffice: UserOffice
    },
function (data) { ....
      }
});

If calling an internal method inside a controller, then you'd just call it as if invoking another method call inside the controller.

A further point raised is that you may have your js inside its own file, in which case, you can't use the @Url.Action() stuff. In this case, I'd advise using the jquery data-attributes and placing the target URL onto one of your key dom elements and then targetting that from your js using the .data('your-attr') from the source file. Works very well and is definitely my recommendation.

[edit] - althought the question is def related to WebClient, i think it's worth demonstrating what I mean by using the data-attributes for the external js implementation:

//the cshtml code
<button id="myUpdateButton" data-target-url="@Url.Action("youraction", "controller")">

//the js code (assumes click on myUpdateButton)
var targetUrl = $(this).data('target-url');
// then use targetUrl in place of the 1st param in the `$.post` method.

Anyway, just me being ott :-)

3 Comments

Note, the Url.Action technique only works if your javascript is not in a .js file (it needs to be in a .cshtml file).
I want to post to a Web API from MVC controller (c#).
ok, in that case, and assuming a differtent project, just treat as if it's an extermnal resource and use the WebClient lib
0

As others have suggested you could use the WebClient class. There are a couple of alternatives you could also try. Take a look at HttpClient and RestSharp, both available via nuget. I've personally used RestSharp with a number of 3rd party APIs as well as my own APIs to great effect.

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.