How can I get .cshtml from Database dynamically

2019-04-10 03:21发布

问题:

I have to say: This is a different question from these:

  • http://craftycodeblog.com/2010/05/15/asp-net-mvc-render-partial-view-to-string/

  • ASP.NET MVC Razor: How to render a Razor Partial View's HTML inside the controller action

  • http://blog.rocketbase.co.uk/2011/04/asp-net-mvc-virtual-path-provider/

  • ASP.NET MVC load Razor view from database

I want to provide dynamic .cshtml Content from db when i want.

Example:

City Table,FreeHtml Column

@model City 
@Html.CheckBox - @Model.Name - @Html.CustomHelper

How can I write as html helper:


@model City
@Html.RazorRaw(Model.FreeHtml,Model)

or as CustomViewResult


public RazorPartialViewResult CityHtml(City city)
{
   return new RazorPartialViewResult(city.FreeHtml,city)
}

回答1:

I think your answer is here: Storing ASP.Net MVC Views in the Database

You need to replace the default view engine(s) with a custom one. Here is an example: http://weblogs.asp.net/imranbaloch/archive/2011/06/27/view-engine-with-dynamic-view-location.aspx