This code allows you to render a mvc action as a string. So it can be used in ASP.Net webForms and so on.
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using MvcApplication1.Controllers;
namespace MvcApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("<pre>" + HttpUtility.HtmlEncode(MVCHelper.RenderAction<HomeController>("Index")) + "</pre>");
}
}
public static class MVCHelper
{
public static string RenderAction<TController>(string strActionName, RouteValueDictionary dicRouteValueDictionary = null) where TController : Controller
{
return RenderAction(HttpContext.Current, GetControllerName<TController>(), strActionName, dicRouteValueDictionary);
}
public static string RenderAction<TController>(HttpContext objHttpContext, string strActionName, RouteValueDictionary dicRouteValueDictionary = null) where TController: Controller
{
return RenderAction(objHttpContext, GetControllerName<TController>(), strActionName, dicRouteValueDictionary);
}
public static string RenderAction(HttpContext objHttpContext, string strControllerName, string strActionName, RouteValueDictionary dicRouteValueDictionary = null)
{
if (dicRouteValueDictionary == null)
dicRouteValueDictionary = new RouteValueDictionary();
RouteData objData = new RouteData();
objData.Values["controller"] = strControllerName;
objData.Values["action"] = strActionName;
foreach (KeyValuePair<string, object> objPair in dicRouteValueDictionary)
objData.Values[objPair.Key] = objPair.Value;
HttpContextBase objContext = new HttpContextWrapper(objHttpContext);
RequestContext objRequestContext = new RequestContext(objContext, objData);
IControllerFactory objControllerFactory = ControllerBuilder.Current.GetControllerFactory();
IController objController = objControllerFactory.CreateController(objRequestContext, strControllerName);
string strResult = objController.ExecAsString(objRequestContext);
objControllerFactory.ReleaseController(objController);
return strResult;
}
private static string GetControllerName<TController>() where TController : Controller
{
const string POSTFIX = "Controller";
string strControllerName = typeof (TController).Name;
return strControllerName.EndsWith(POSTFIX) ? strControllerName.Remove(strControllerName.Length - POSTFIX.Length) : strControllerName;
}
private static string ExecAsString(this IController objController, RequestContext objRequestContext)
{
using (ResponseStringRedirect objRedirect = new ResponseStringRedirect(objRequestContext.HttpContext.Response))
{
objController.Execute(objRequestContext);
return objRedirect.ToString();
}
}
private class ResponseStringRedirect : IDisposable
{
private readonly HttpResponseBase _objResponse;
private readonly TextWriter _objResponseWriter;
private StringWriter _objStringWriter;
public ResponseStringRedirect(HttpResponseBase objResponse)
{
_objResponse = objResponse;
_objResponseWriter = objResponse.Output;
_objStringWriter = new StringWriter();
objResponse.Output = _objStringWriter;
}
public override string ToString()
{
_objStringWriter.Flush();
return _objStringWriter.ToString();
}
public void Dispose()
{
if (_objStringWriter == null)
return;
_objStringWriter.Dispose();
_objStringWriter = null;
_objResponse.Output = _objResponseWriter;
}
}
}
}
Kommentare zum Snippet