How to render an MVC action to string without HTTP context? - model-view-controller

I need to render MVC action without HTTP context (I'm using quartz.net scheduling library, I'd like to render the action to string in quartz job).
How do I create an HTTP context, instantiate MyController and execute MyController.MyAction() so I get it in the quartz job as string?
Edit 1:
Problem is non trivial. I looked for other posts on stack overflow but all of them seem to cover the situation where you are wihtin a context of web request. In my case HttpContext.Current is null.

I've been using Razor Engine successfully for a while but it has some limitations; you can't use partial views, it's not thread-safe and most damning, studio doesn't recognize your .cshtml files as razor files since you're not really in an ASP.net MVC project so you get no intellisense.
I am therefore also looking for a better solution as well. Have you found any?
Ideally I'd want to be able to directly call a controller action and get the html.

Related

Is it possible to modify the output of the razor viewengine just before sending it to the client?

Im building an ASP.NET MVC 3 app using Razor as template language.
Here is what I would like to do:
When all template content from cshtml-files for a certain request have been parsed in razor viewengine and ready to output to the visitor - then I would like to insert some extra information into the parsed html content.
So my question is:
Is there an event of any kind to hook on to inside the Razor viewengine or inside the MVC framework that allows me to do this kind of changes to the output?
Don't ask why I'd want to do something like this in a MVC application, it's a long and boring story.
Given the vagueness of your question, perhaps this article might help, or maybe you can specify in what way the article isn't helpful...
Dependency Injection in ASP.Net MVC Views
UPDATE:
How about an ActionFilter? I seem to recall that you can get the viewresult and tinker with it in an ActionFilter, although I have only ever done this for a json transformation. Here's another article: Use ASP.Net action filters to render
Here's a SO answer--this might be a duplicate question!

A good substitute for ASMX web service methods, but not a general handler

The best thing I like about ASP.NET MVC, is that you can directly call a server method (called action), from the client. This is so convenient, and so straightforward, that I really like to implement such a model in ASP.NET WebForms too.
However, in ASP.NET WebForms, to call a server method from the client, you should either use Page Methods, or Web Services, both of which use SOAP as their communication protocol (though JSON can also be used).
There is also another substitution, which is using Generic Handlers. The problem with them however is that, a separate Generic Handler should be written for each server method. In other words, each Generic Handler works like a simple method.
Is there anyway else to imitate MVC model in ASP.NET WebForms?
Please note that I can't change to MVC platform right now, cause the project at our hand is a big project and we don't have required resources and time to change our platform. What we seek, is a simple MVC model implementation for our AJAX calls. A problem that we have with Web Services, is the known problem of SoapException, and we're not interested in creating custom SoapExctensions.
You can mix ASP.NET MVC and ASP.NET Webforms in the same project. You'll just need to add the correct MVC parts to your current Webforms project and have the best of both worlds.
http://www.aspnetmvcninja.com/general/mixing-asp-net-mvc-and-webforms has a good walkthrough of all the steps you'll need to take to get it working.
You can splice your asp.net webforms app with something like Nancy perhaps?
I've had great success with Nancy and Knockout after I abandoned the horrible Ajax Control Toolkit.
(Apologies if I have misunderstood your question - I read it twice)

How does MVC3 picks which ViewEngine to use if I have multiple engines in ViewEngines collection?

I have a custom view engine developed internally. In the same project, I would like to use Razor for some pages and my custom engine for some pages. How does MVC framework choose which engine to use? BTW, my custom engine does not require any templates, it renders pages based on meta-data from database. For my custom engine I don't want to setup any template files.
What I am expecting is there should be a way to drive framework to use certain engine based on the controller name and action name.
Is this flexibility exists in MVC3?
Your view engine should implement the IViewEngine interface. After you registered your view engine with the ViewEngines.Engines.Add() method, the MVC framework will call FindView and FindPartialView whenever it needs a view engine to render a view.
It's absolutely possible for multiple view engines to operate side by side. If you don't want your view engine to be used in a specific situation you return new ViewEngineResult(new string[0]); from FindView or FindPartialView and MVC will choose another view engine. If you do want your view engine to be used you return a valid ViewEngineResult pointing to the view class (that is implementing IView) that you want to Render the result.
There are some specifics with the useCache parameter. If you want to know more, there was an excellent presentation on building your own view engine at TechEd 2011 by Louis DeJardin. You can find the video of Writing an ASP.NET MVC View Engine at Channel9.
I think the easiest way would be to implement a IViewPageActivator, http://bradwilson.typepad.com/blog/2010/10/service-location-pt11-view-page-activator.html and http://msdn.microsoft.com/en-us/library/system.web.mvc.iviewpageactivator(v=vs.98).aspx.
I think that returning null from the Create method will make it later default to the default IViewPageActivator. You inject it in the DependencyResolver, http://bradwilson.typepad.com/blog/2010/10/service-location-pt5-idependencyresolver.html.
It might be easier to use if you are using a dependency injection framework as NInject or Unity.

Is there a better way to implement forms than using RenderAction?

I am working on a .Net MVC2 project where I am trying to modularize functionality as much as possible. One aspect of this would be to place a form in a partial views which is then called using RenderAction. This is so that the form's GET, POST, validation and redirection can be handled independently of the parent view.
I have read blogs and forums etc that suggest that this is not ideal behaviour for MVC, however I cannot think of a way round it. The main issue I am having is redirecting from the rendered action and I understand why I cannot do this.
What I need to know is this: if I can't and shouldn't code my modules in this way, how should I code them so they are independent of the parent view?

Render ASP.NET MVC string to View without HttpContext or ControllerContext?

I need to render an ASP.NET MVC view to a string so as to be able to send it via an email (it is an order confirmation email defined in an .ascx file ).
I've successfully been able to render an ASP.NET MVC View to string using one of the methods in this question.
However now I need to be able to do it via a WCF service (that will be accessed via silverlight) and so I don't have a ControllerContext. This WCF service is contained within the same project as my MVC project so has access to all my models etc.
I've looked at several questions on Stackoverflow about this issue, but they all seem to need a controller context. I thought there was something in mvccontrib but it doesn't seem to be there anymore.
The closest I've found is the accepted answer to the aforementioned question, but it unfortunately breaks with RenderPartial within the view you're rendering.
I'm hoping maybe some of the behind the scenes work for ASP.NET MVC 2 related to RenderAction may help make this possible now?
BuildStarted and I have put together a standalone Razor templating engine which you might find useful (Github). You'll need to adapt your .ascx content into a simple string template, but it should do the job.
If you've got NuGet, you can run Install-Package RazorEngine
You may checkout the following blog post. Also Postal is worth looking at.
You need to create a fake HttpContextBase with a fake HttpRequestBase that return meaningful values from their properties.

Resources