什么是RUNAT的状态="服务器"标签在ASP.NET MVC?状态、标签、服务器、QUOT

2023-09-02 20:47:16 作者:啦啦啦~俄是卖萌的小行家

在ASP.NET MVC态的文字,有没有RUNAT服务器标签,连这MSDN文章说这个的时候,对上面的这个句子有一个与在头上RUNAT服务器标签的code为例元素:

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:

http://msdn.microsoft.com/en-us/magazine/cc337884.aspx

和对计算器的谈话我读

您要的其实使用   RUNAT =服务器的控制意味着你   应该做一个传统的ASP.NET   应用程序。

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

和存在的ContentPlaceHolders RUNAT服务器属性。

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

我看不在ASP.NET MVC的RUNAT服务器方面的唯一的事情是无处不在FORM =服务器上的每个.aspx页/视图标签。

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,其余的时候,他们说,ASP.NET MVC没有这些人们有什么样的意思?

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?

推荐答案

如果您使用的是=服务器标签的任何元素,如DIV它都会使该code作为一个单独的方法在编译的页面中。

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.

如果你正在将'传统'code的一个好主意,删除所有RUNAT标签直到你最终在一个情况下code像下面给你一个错误,否则前面

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.

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

这code将无法告诉你关于有些疯狂猫被淘汰的范围。最后,当你看完整的生成code,你将看到&LT; D​​IV&GT; 已生成为其整个自己的方法 - 这当然是不同的范围,看不到猫。

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.

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

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

您将看到当前模板为您提供了本作的

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>

这让我想知道 - 如果我们使用&LT;%=语法直接写入标题到标题标记 - 那么为什么我们需要它RUNAT?

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?

原来我怀疑是codebehind为查找标题标签内的现有价值(这将是输出这里由&LT;%= Html.En code(计算机[标题])%&GT; 如果找到一个(这将是在所有样本意见的情况下, MVC模板),那么它不会做任何进一步的,如果没有标题存在(如果计算机[标题]为null或空),则默认为无论是在你看来由标题属性:

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 :

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

在我的母版页,我会删除了 RUNAT =服务器标签 - 因为我不认为我会永远想填充从视图的我的页面标题标题属性。但我持观望态度做关于这个问题的这一悬而未决菲尔的承诺的博客文章 - 万一RUNAT服务器给我有用的东西为我的CSS和JS太

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.