Search code examples
.netasp.net-mvc-4razorengine

HTML page integrate with razor engine layout view in MVC


We are developing a web application in MVC4. The layout view name is layout.cshtml which is cshtml file (Razor engine) and we did several essential things (such as login div) in layout view. Recently a requirement has met that we need to integrate several static HTML page with this MVC4 web application. The problem that we face is,

  • It is compulsory that users who view the static HTML also need to access to this login div. So the master pages of all those static HTML must be layout.cshtml.
  • Since we code several other views in this application which depends on this layout, we can't change the layout from CSHTML to HTML.

Considering these scenario, how can we integrate those HTML pages having master page as layout.cshtml with this web application. Thanks in advance.


Solution

  • You can embed partial view partial view demo with the Html (only the html into body), it is one clean way to achieve what you want if I did well understood your problem.

    You just ave to put the static html into "_static.cshtml" without razor if you want and then include it by using

    @Html.Partial("_static")
    

    I also recommand you to make partialview with the login form too, your layout.cshtml could be more clear for editing

    If you want to handle lots of partial, then you can use only one controller and use one string.. something like this:

    List of available pages

    <ul>
        <li>
            @Html.ActionLink("page1", "GetStatic", "Home", new { name = "page1" }, null)
        </li>
        <li>
            @Html.ActionLink("page2", "GetStatic", "Home", new { name = "page2" }, null)
        </li>
    </ul>
    

    Controller hitten by clicking on list items:

    public ActionResult GetStatic(string name)
            {
                if(name .IsNotNull())
                {
                    return View(model:name);
                }
                return RedirectToAction("Index");
            }
    

    GetStatic.cshtml:

    @model String
    
    @Html.partial("@Model")