Make an http request through C# code? [duplicate]
Possible Duplicate:
Render a view as a string
Hi
I am wondering is it possible to have in you C# code(through a scheduler that is on it's own thread and has no knowledge of httpcontext) a request that goes to a controller ?
//server side code
// do calculations
// post to a controller that takes in a list of view models
// do stuff with the collection of view models.
public myControllerIwantToCallFromServerSide(List<VM> viewModels)
{
// stuff here
}
I need some way to do an http request so that I can get a httpcontext as I need to a live http context to use a library(action mailer) that takes 开发者_C百科an mvc view and renders it into a email and sends it.
You could use the the WebClient class:
using (var client = new WebClient())
{
var values = new NameValueCollection
{
{ "prop1", "value 1" },
{ "prop1", "value 2" },
};
var result = client.UploadValues("http://example.com/", values);
}
I use the method Richard posted. I call the viewengine to render my partial view, then I use System.Net.Mail.SmtpClient
to generate the email and send it off.
I apologize, as I would preferrably comment above, but I do not have the reputation to do so.
精彩评论