Is it possible to modify the output of the razor viewengine just before sending it to the client? - asp.net-mvc-3

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!

Related

How to render an MVC action to string without HTTP context?

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.

Create custom controls in asp mvc 3

I have to create custom controls in ASP.NET MVC 3. Controls like customized buttons which I will put in DLL and reuse in any project.
Do you know a good way or maybe you can give me a good tutorial to do this.
Thanks.
You can't, WebControls are for Web Forms. In asp.net mvc anything view related(html, javascript, css) is decoupled from the controller. The best you can do is to have helpers which are simply extension methods. But something similar to the webcontrols is specific to web forms only.
This post and this should give you an idea
You would need to use Partial Views to re-use code.
http://rachelappel.com/razor/partial-views-in-asp-net-mvc-3-w-the-razor-view-engine/

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)

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