Is Razor view with ASPX .Master page possible?

2019-01-22 18:02发布

问题:

Is it possible to keep my existing .master page and have it used with a new ASP.NET MVC 3 Razor view? I tried this:

@{
   LayoutPage = "~/Views/Shared/Site.master";
 }

And it gives me this error message:

The file '~/Views/Shared/Site.master' could not be rendered, because it does not exist or is not a valid page.

回答1:

Unfortunately no. Master pages are a part of the ASPX WebForms view engine, not the MVC framework so Razor cannot interoperate with it.

One option would be to duplicate the masters, as you mentioned, but rather than copying all the code, you could factor the Master page into a bunch of ASPX partials that the Razor and ASPX masters could embed. Then you can start converting each page and partial, one-by-one, to Razor and eventually get rid of the ASPX master.



回答2:

There is actually a way to do this. Scott Hansleman has a blog post on the topic: http://www.hanselman.com/blog/MixingRazorViewsAndWebFormsMasterPagesWithASPNETMVC3.aspx

It's a bit hackish, but doable. I think the approach described could be encapsulated and cleaned up even further so that you could build your views and controllers without worrying about how things are being wired together.



回答3:

I think you need to look for _Layout.cshtml in the shared folder...

Here is the comparison between aspx and razor view engine....

http://weblogs.asp.net/shijuvarghese/archive/2010/07/29/aspx-and-razor-view-engines-in-asp-net-mvc-3-preview-1.aspx

this is also an interessting post about nested masterpages with razor...

http://weblogs.asp.net/fredriknormen/archive/2010/08/01/asp-net-mvc-3-0-preview-1-razor-and-nested-master-pages.aspx

HTH



回答4:

Support for .NET User Controls in MVC

MVC does not officially support .Net User Controls but you can retrieve the html produced by them. The following code retrieves the HTML produced from a page made up of dozens of ASCX files.

  • Create an ASP page that contains your .Net User Controls within your MVC site.
  • Get the HTML string produced by your user controls.
  • Use the HTML string in your MVC Layout page.

Code example:

try{

    using (WebClient client = new WebClient())  
    {

        client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";

        myHTML = client.DownloadString("http//www.mysite.com/header.aspx");
    }

} catch ( WebException exception )

{

    using(var reader = new StreamReader(exception.Response.GetResponseStream())){

        Response.Write(reader.ReadToEnd());

    }

}

@Html.Raw(myHTML ); //OR Response.Write(myHTML);

@RenderBody(); 


回答5:

Having just been through this process myself, I found that this method by Matt Hawley worked a treat.

This approach works by creating a standard aspx page that uses your required master page. You can then add content placeholders as required. You then call RenderPartial with the name of the view to use. The response from your controller then gets passed on down to the actual view you want to render.

There is a bit more plumbing required to make this work and you have to call an extensions method to render your view in the controller but once you are setup it works very well.



回答6:

In Razor you can achieve the same functionality using Layout pages.

Layout/Master pages using Razor