asp.net-mvc ASPX .Master 页面的 Razor 视图是否可行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3479094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is Razor view with ASPX .Master page possible?
提问by bkaid
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:
是否可以保留我现有的 .master 页面并将其与新的 ASP.NET MVC 3 Razor 视图一起使用?我试过这个:
@{
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.
无法呈现文件“~/Views/Shared/Site.master”,因为它不存在或不是有效页面。
采纳答案by Andrew Stanton-Nurse
Unfortunately no. Master pages are a part of the ASPX WebForms view engine, not the MVC framework so Razor cannot interoperate with it.
抱歉不行。母版页是 ASPX WebForms 视图引擎的一部分,而不是 MVC 框架,因此 Razor 无法与其进行互操作。
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.
一种选择是复制母版,正如您提到的,但不是复制所有代码,您可以将母版页分解为 Razor 和 ASPX 母版可以嵌入的一堆 ASPX 部分。然后,您可以开始将每个页面和部分页面一一转换为 Razor,并最终摆脱 ASPX 母版。
回答by Matt Honeycutt
There is actually a way to do this. Scott Hansleman has a blog post on the topic: http://www.hanselman.com/blog/MixingRazorViewsAndWebFormsMasterPagesWithASPNETMVC3.aspx
实际上有一种方法可以做到这一点。Scott Hansleman 有一篇关于该主题的博客文章: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.
这有点hackish,但可行。我认为所描述的方法可以进一步封装和清理,以便您可以构建视图和控制器,而不必担心事物是如何连接在一起的。
回答by server info
I think you need to look for _Layout.cshtml in the shared folder...
我认为您需要在共享文件夹中查找 _Layout.cshtml ...
Here is the comparison between aspx and razor view engine....
这是aspx和razor视图引擎之间的比较....
this is also an interessting post about nested masterpages with razor...
这也是一篇关于使用剃刀嵌套母版页的有趣帖子......
HTH
HTH
回答by Hyman Nichols
Support for .NET User Controls in MVC
支持 MVC 中的 .NET 用户控件
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.
MVC 不正式支持 .Net 用户控件,但您可以检索它们生成的 html。以下代码检索从由数十个 ASCX 文件组成的页面生成的 HTML。
- 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.
- 在 MVC 站点中创建一个包含 .Net 用户控件的 ASP 页面。
- 获取用户控件生成的 HTML 字符串。
- 在 MVC 布局页面中使用 HTML 字符串。
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();
回答by Derek Ekins
Having just been through this process myself, I found that this method by Matt Hawleyworked a treat.
我自己刚刚经历了这个过程,我发现Matt Hawley 的这个方法很有效。
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.
这种方法的工作原理是创建一个使用所需母版页的标准 aspx 页面。然后,您可以根据需要添加内容占位符。然后使用要使用的视图的名称调用 RenderPartial。来自控制器的响应然后被传递到您想要呈现的实际视图。
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.
完成这项工作需要更多的管道,您必须调用扩展方法来在控制器中呈现您的视图,但是一旦设置好它就可以很好地工作。
回答by mahesh
In Razor you can achieve the same functionality using Layout pages.
在 Razor 中,您可以使用布局页面实现相同的功能。