ASP.NET MVC中runat =“server”标签的状态是什么?

时间:2021-11-10 15:57:39

Some texts on ASP.NET MVC state that "there are no runat server tags", even this MSDN article says this, when, right above that statement there is a code example with a runat server tag in the HEAD element:

ASP.NET MVC上的一些文章指出“没有runat服务器标签”,即使这篇MSDN文章也说过,在该语句的正上方,HEAD元素中有一个带有runat服务器标签的代码示例:

And on * conversations I read

在我读过的*对话中

"The fact that you want to use "runat=server" controls means that you should be doing a traditional ASP.NET app.

“你想使用”runat = server“控件的事实意味着你应该做一个传统的ASP.NET应用程序。

And of course in the Site.Master page there are runat server attributes in the ContentPlaceHolders.

当然,在Site.Master页面中,ContentPlaceHolders中有runat服务器属性。

The only thing I see absent from ASP.NET MVC in terms of runat server is the ubiquitous FORM runat="server" tag on each .aspx page/view.

在runat服务器方面,我唯一看不到ASP.NET MVC的是每个.aspx页面/视图上无处不在的FORM runat =“server”标签。

But what about the rest of the runat server tags in ASP.NET MVC, what do people mean when they say that ASP.NET MVC does not have these?

但是ASP.NET MVC中的其余runat服务器标签怎么样呢?当人们说ASP.NET MVC没有这些时,人们的意思是什么?

4 个解决方案

#1


7  

If you use a runat="server" tag on ANY element, such as a DIV it will render that code as a separate method in the compiled page.

如果在任何元素(例如DIV)上使用runat =“server”标记,它将在编译页面中将该代码呈现为单独的方法。

If you're converting 'legacy' code its a good idea to remove all runat tags right up front otherwise you end up in a situation where code like the following gives you an error.

如果您正在转换“遗留”代码,最好立即删除所有runat标记,否则最终会出现以下代码导致错误的情况。

<% foreach (var cat in cats) { %>
    <div runat="server">
         <span class="name"> <%= cat.name %> </span> is a
         <span class="breed"> <%= cat.breed %> </span>
    </div>
 <% } %>

This code will fail telling you some craziness about 'cat' being out of scope. Eventually when you look at the full generated code you'll see that the <div> has been generated as its whole own method - which is of course a different scope with no cats in sight.

这段代码无法告诉你一些关于'cat'超出范围的疯狂。最后,当你查看完整生成的代码时,你会看到

已经被生成为它自己的整个方法 - 这当然是一个不同的范围,看不到猫。

Back for a second to the default template for an MVC application:

返回MVC应用程序的默认模板:

You'll see the current template gives you this for the head :

你会看到当前的模板为你提供了这个:

<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title><%= Html.Encode(ViewData["Title"]) %></title>
    <link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
</head>

This left me wondering -- if we're using <%= syntax to write the title directly into the title tag - then why would we need to make it runat?

这让我想知道 - 如果我们使用<%=语法将标题直接写入标题标签 - 那么为什么我们需要让它运行?

It turns out as I suspected that the codebehind for head looks for an existing value inside the title tag (which would have been output here by <%= Html.Encode(ViewData["Title"]) %>. If it finds one (which will be the case for the all sample views in the MVC template) then it won't do anything further. If no title exists (if ViewData["Title"] is null or empty) it will default to whatever is defined in your view by the Title attribute :

事实证明,我怀疑head的headbehind在title标签内寻找一个现有值(这里可以通过<%= Html.Encode(ViewData [“Title”])%>输出。如果找到一个(这将是MVC模板中所有示例视图的情况)然后它将不会做任何进一步的操作。如果没有标题存在(如果ViewData [“Title”]为null或为空)它将默认为您的中定义的任何内容按标题属性查看:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/RRMaster.Master" 
Title="View Products" AutoEventWireup="true" CodeBehind="ViewProduct.aspx.cs"
Inherits="RR_MVC.Views.Products.ViewProduct" %>

In my master page I would have removed the runat='server' tag - since I dont think I'll ever want to populate my page title from the view's Title property. But I'm holding off doing this pending Phil's promised blog post on the subject - in case the runat server gives me anything useful for my CSS and JS too.

在我的母版页中,我会删除runat ='server'标签 - 因为我不认为我会想要从视图的Title属性中填充我的页面标题。但是我暂时不做这个有关这个主题的菲尔承诺的博客文章 - 如果runat服务器给了我任何对我的CSS和JS有用的东西。

#2


2  

They don't mean that you can't use runat="server", but rather that it not necessary to use server-side controls, generally, in MVC. If you find that you need a server-side control and are working with it in code-behind that's and indication that the application is tending back toward webforms. All of the stuff that would normally happen in your code-behind shoulo now be handled in your controller or in the view logic itself.

它们并不意味着您不能使用runat =“server”,而是通常在MVC中不必使用服务器端控件。如果您发现需要服务器端控件并在代码隐藏中使用它,并且表明应用程序正在趋向于webforms。通常在您的代码隐藏shoulo中发生的所有事情现在都可以在您的控制器或视图逻辑本身中处理。

#3


1  

Just read in this tutorial:

请阅读本教程:

http://www.asp.net/learn/mvc/tutorial-12-cs.aspx

http://www.asp.net/learn/mvc/tutorial-12-cs.aspx

that you need the

你需要的

<head runat="server">

to be able to easily define the page title in your views.

能够在您的视图中轻松定义页面标题。

#4


1  

MVC is just a layer on top of webforms. My custom webforms controls also require the head tag to be accessible serverside for script registration. These custom controls are rendered clientside and don't use viewstate or serverside events. Because of this they can also be used in MVC with the ASPX view engine.

MVC只是webforms之上的一层。我的自定义webforms控件还要求head标签可以访问服务器端以进行脚本注册。这些自定义控件呈现在客户端,不使用viewstate或serverside事件。因此,它们也可以在带有ASPX视图引擎的MVC中使用。

#1


7  

If you use a runat="server" tag on ANY element, such as a DIV it will render that code as a separate method in the compiled page.

如果在任何元素(例如DIV)上使用runat =“server”标记,它将在编译页面中将该代码呈现为单独的方法。

If you're converting 'legacy' code its a good idea to remove all runat tags right up front otherwise you end up in a situation where code like the following gives you an error.

如果您正在转换“遗留”代码,最好立即删除所有runat标记,否则最终会出现以下代码导致错误的情况。

<% foreach (var cat in cats) { %>
    <div runat="server">
         <span class="name"> <%= cat.name %> </span> is a
         <span class="breed"> <%= cat.breed %> </span>
    </div>
 <% } %>

This code will fail telling you some craziness about 'cat' being out of scope. Eventually when you look at the full generated code you'll see that the <div> has been generated as its whole own method - which is of course a different scope with no cats in sight.

这段代码无法告诉你一些关于'cat'超出范围的疯狂。最后,当你查看完整生成的代码时,你会看到

已经被生成为它自己的整个方法 - 这当然是一个不同的范围,看不到猫。

Back for a second to the default template for an MVC application:

返回MVC应用程序的默认模板:

You'll see the current template gives you this for the head :

你会看到当前的模板为你提供了这个:

<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title><%= Html.Encode(ViewData["Title"]) %></title>
    <link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
</head>

This left me wondering -- if we're using <%= syntax to write the title directly into the title tag - then why would we need to make it runat?

这让我想知道 - 如果我们使用<%=语法将标题直接写入标题标签 - 那么为什么我们需要让它运行?

It turns out as I suspected that the codebehind for head looks for an existing value inside the title tag (which would have been output here by <%= Html.Encode(ViewData["Title"]) %>. If it finds one (which will be the case for the all sample views in the MVC template) then it won't do anything further. If no title exists (if ViewData["Title"] is null or empty) it will default to whatever is defined in your view by the Title attribute :

事实证明,我怀疑head的headbehind在title标签内寻找一个现有值(这里可以通过<%= Html.Encode(ViewData [“Title”])%>输出。如果找到一个(这将是MVC模板中所有示例视图的情况)然后它将不会做任何进一步的操作。如果没有标题存在(如果ViewData [“Title”]为null或为空)它将默认为您的中定义的任何内容按标题属性查看:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/RRMaster.Master" 
Title="View Products" AutoEventWireup="true" CodeBehind="ViewProduct.aspx.cs"
Inherits="RR_MVC.Views.Products.ViewProduct" %>

In my master page I would have removed the runat='server' tag - since I dont think I'll ever want to populate my page title from the view's Title property. But I'm holding off doing this pending Phil's promised blog post on the subject - in case the runat server gives me anything useful for my CSS and JS too.

在我的母版页中,我会删除runat ='server'标签 - 因为我不认为我会想要从视图的Title属性中填充我的页面标题。但是我暂时不做这个有关这个主题的菲尔承诺的博客文章 - 如果runat服务器给了我任何对我的CSS和JS有用的东西。

#2


2  

They don't mean that you can't use runat="server", but rather that it not necessary to use server-side controls, generally, in MVC. If you find that you need a server-side control and are working with it in code-behind that's and indication that the application is tending back toward webforms. All of the stuff that would normally happen in your code-behind shoulo now be handled in your controller or in the view logic itself.

它们并不意味着您不能使用runat =“server”,而是通常在MVC中不必使用服务器端控件。如果您发现需要服务器端控件并在代码隐藏中使用它,并且表明应用程序正在趋向于webforms。通常在您的代码隐藏shoulo中发生的所有事情现在都可以在您的控制器或视图逻辑本身中处理。

#3


1  

Just read in this tutorial:

请阅读本教程:

http://www.asp.net/learn/mvc/tutorial-12-cs.aspx

http://www.asp.net/learn/mvc/tutorial-12-cs.aspx

that you need the

你需要的

<head runat="server">

to be able to easily define the page title in your views.

能够在您的视图中轻松定义页面标题。

#4


1  

MVC is just a layer on top of webforms. My custom webforms controls also require the head tag to be accessible serverside for script registration. These custom controls are rendered clientside and don't use viewstate or serverside events. Because of this they can also be used in MVC with the ASPX view engine.

MVC只是webforms之上的一层。我的自定义webforms控件还要求head标签可以访问服务器端以进行脚本注册。这些自定义控件呈现在客户端,不使用viewstate或serverside事件。因此,它们也可以在带有ASPX视图引擎的MVC中使用。