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.

Community
  • 1
  • 1
chobo2
  • 83,322
  • 195
  • 530
  • 832
  • It's certainly possible, it just seems like a bad idea. – Roman May 20 '11 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. – chobo2 May 20 '11 at 16:18
  • @Richard - I believe what you posted needs a live http context what I don't have. – chobo2 May 20 '11 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? – Roman May 20 '11 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. – chobo2 May 20 '11 at 17:49
  • You may want to include that information in the question. – Roman May 20 '11 at 19:01

2 Answers2

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);
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
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.

eppdog
  • 423
  • 2
  • 11
  • 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. – chobo2 May 20 '11 at 17:31