2

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 an mvc view and renders it into a email and sends it.

6
  • It's certainly possible, it just seems like a bad idea. Commented May 20, 2011 at 16:15
  • @R0MANARMY - Ya I know it does not seem ideal but I am looking at few choices do something like that or I have to make my own smtp email sender and have to try to make these email sent look the same as the one from this library expect I lose the mvc views and master pages or rip out all my emails I made so far with the library and start all over. So unless someone can tell me something better I am choosing what seems to be the best out of the worst cases. Commented May 20, 2011 at 16:18
  • @Richard - I believe what you posted needs a live http context what I don't have. Commented May 20, 2011 at 17:30
  • You keep repeating that you don't have an http context, but if you're inside an action you can get a hold of it. Are you not doing this from an ASP.NET action? Commented May 20, 2011 at 17:34
  • @R0MANARMY - No i don't have a live http context. I am starting a quartz scheduler through application start that runs in it's own thread and does not make a http context. Commented May 20, 2011 at 17:49

2 Answers 2

7

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);
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

I think you need a http context to use that. If that worked then I could use the library I am using since that's pretty much what it is doing. It takes a view renders it and sends it off.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.