asp.net server control in mvc application
We have a server control that we want to use in our .net MVC application. The control will only be a part of the page and I would like to not refactor the whole application (including layout pages) to get this one page to work. So I believe my choices to be as follows.
- Just put everything on an aspx page, and use routing to go there, losing my layout pages
- Use an iframe (lame)
- Use HttpContext.Current.Server.Execute(url) (throws error)
- Use an http request to get the html and then shove it on the page, better than an iframe
So idealy I would like to use HttpContext.Current.Server.Execute to execute the url, return the html and do that all server side. I just keep getting an error thrown. The page is accessible from a browser, but the routing is taking affect sometimes and then I just get a "Child request failed".
Thoughts? Please also don't say this is a bad idea, I already know that. I'm just trying to 开发者_开发知识库make the best of a bad situation.
Thanks.
I ended up having my asp.net web form page grab the MVC view, and find out where to put it's content. It's backwards that way but works and the postbacks work as well. This was done for a Razor engine with an Action and view, not a partial view.
var controller = new CalendarController();
var routeData = new System.Web.Routing.RouteData();
routeData.Values.Add("controller", "CalendarController");
controller.ControllerContext = new ControllerContext(new FakeHttpContext(), routeData, controller);
try
{
using (StringWriter sw = new StringWriter())
{
var viewResult = controller.Index() as ViewResult;
var view = ViewEngines.Engines.FindView(controller.ControllerContext, "relative_pathto_view", "");
ViewContext viewContext = new ViewContext(controller.ControllerContext, view.View, controller.ViewData, controller.TempData, sw);
view.View.Render(viewContext, sw);
var page = sw.GetStringBuilder().ToString();
}
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
精彩评论