Convert server side to razor

I am updating / modifying an ASP.NET web form application on MVC5 - ideally - the Razor engine (to allow automatic page switching in the layout and take advantage of MVC features).

We have hundreds of server applications, all with the name * .inc. Absolute heaps. The application is so large that we have to convert pages in parts - there is no way to do it right away. (To get an idea of ​​how big this project is and how long it took, the whole history of version control in VSS when we migrated to TFS was about 10 GB. That includes a lot of media files, but still. Away.)

Is there any way to convert these #! include calls to a call to RenderPartial () or RenderAction () without renaming each file in .cshtml?

Duplicating an include file is a bad recommendation because it violates DRY.

Ideally, I would like one partial view to handle this - I assume that it will take the file name as an argument and something single-line so that I can train everyone to convert them trivially.

I also want file level caching. Just loading output from disk and throwing it into the output stream is nice for small sites ... but it's not a small site.

We have an ascx file that now caches many of them, but I don’t know how to write the appropriate one in Razor.

Edit 1: they mostly include the non-code, but I'm sure the definition of the user control is a bit quiet. I would have to rewrite them, so suppose the answer is "it's pure html, no code"

2: , include ( , ). include . ASCX, , .

3: couttown Layout. -.

+4
3

4 .

. , .


/ include :

public class IncludeController : Controller
{
    // GET: Render
    public ActionResult Render(string path)
    {
        return new IncludeResult(path);
    }
}

public class IncludeResult : ActionResult
{
    private string _path { get; set; }

    public IncludeResult(string path)
    {
        _path = path;
    }
    public override void ExecuteResult(ControllerContext context)
    {
        // possibly check the cache here, or let MVC handle cache at the action level
        // get the contents of the include file output to the response
        context.HttpContext.Response.Write(File.OpenRead(_path));
    }
}

@Html.Action("Render", "Include", new {path = "~/Includes/test.inc"})

. FilePathResult , , , IncludeResult


HtmlHelper:

public static class HtmlExtension
{
    public static MvcHtmlString RenderInclude(this HtmlHelper helper, string path)
    {
        // check the cache
        string output = File.ReadAllText(path);
        return new MvcHtmlString(output);
    }
}

@Html.RenderInclude("~/includes/test.inc")

, , , , , " MVC"


RazorViewEngine, include:

, , , , , : http://weblogs.asp.net/imranbaloch/view-engine-with-dynamic-view-location

Html.Partial

@Html.Patial("~/includes/test.inc");

include :

, .

public class ViewModel
{
    public MvcHtmlString IncludeContents { get; set; }
    ....

    private GetIncludeContent(string path)
    {
        IncludeContents = new MvcHtmlString(File.ReadAllText(path));
    }
}

@model.IncludeContents
+4

:

- (BuildProviders):

<system.web>
  <compilation debug="true" targetFramework="4.5">
    // Start (Build Provider Information)
    <buildProviders>
      <add extension=".inc" 
           type="System.Web.WebPages.Razor.RazorBuildProvider, System.Web.WebPages.Razor"/>
    </buildProviders>
    // End (Build Provider Information)
  </compilation>
</system.web>

Runtime , INC . HTML, , -, MVC .

:

public class CustomViewEngine : RazorViewEngine
{
    public CustomViewEngine()
    {
        PartialViewLocationFormats = new[]
        {
            "~/Views/{1}/{0}.cshtml",
            "~/Views/{1}/{0}.vbhtml",
            "~/Views/Shared/{0}.cshtml",
            "~/Views/Shared/{0}.vbhtml",
            "~/Include/{0}.inc"  // Path to your include files
        };

        FileExtensions = new[]
        {
            "cshtml",
            "vbhtml",
            "inc",
        };
    }
}

:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        // Need to let the RazorCodeLanguage it can compile .inc files
        RazorCodeLanguage.Languages.Add("inc", new CSharpRazorCodeLanguage());
        // Need to register the extension with the WebPage Http Handler
        WebPageHttpHandler.RegisterExtension("inc");

        // Create the engine
        var viewEngine = new CustomViewEngine();
        // Clear unused Engines
        ViewEngines.Engines.Clear();
        // Add only the modified razor engine
        ViewEngines.Engines.Add(viewEngine);
    }
}

\Views\Web.config \Include\Web.Config, , .

\Include\Test.inc, :

<div>Some Data</div>

:

@Html.Partial("Test")

( )

@{Html.RenderPartial("Test");}

INC MVC-ized. , MVC .. , .cshtml.

Bonus

RazorViewEngine, \Include\*.inc \Views\Shared\*.cshtml, - @Html.Partial("<include file>"), , . ( ), include, .

+2

Brent WebClient HTML, . SSI, .

public static HtmlString RenderInclude(this HtmlHelper helper, string path)
{
   WebClient client = new WebClient();
   string url = string.Format("{0}://{1}{2}{3}", 
      HttpContext.Current.Request.IsSecureConnection ? "https" : "http", 
      HttpContext.Current.Request.Url.Host, 
      HttpContext.Current.Request.Url.Port == 80 ? "" : ":" + HttpContext.Current.Request.Url.Port.ToString(), 
      path);

  string html = client.DownloadString(url);

  return new HtmlString(html);
}

Razor:

@Html.RenderInclude("/includes/test.inc")

, , SSI .

0

All Articles