如何关闭viewstate?

时间:2022-12-04 07:35:04

Coming from a PHP background I love using clean URLs to grab data from one service to another.

来自PHP背景我喜欢使用干净的URL从一个服务获取数据到另一个服务。

However, on some of my ASP.NET projects I get the horrible ViewState parameter in my URLs.

但是,在我的一些ASP.NET项目中,我在URL中获得了可怕的ViewState参数。

Is there a way to turn this off globally?

有没有办法在全球范围内解决这个问题?

What affect will this have on my ASP.NET app?

这会对我的ASP.NET应用程序产生什么影响?

6 个解决方案

#1


34  

You can turn off viewstate for the whole site like this:

您可以关闭整个网站的viewstate,如下所示:

    <system.web>
<pages enableViewState="false" />

That said, you shouldn't be getting it on the url. ViewState is a hidden field that is sent to the server with a postback (which normally uses post). It keeps the state of the controls when the page was rendered to the client, sending it with each postback. If it works for the application you could switch to use post instead (the problem form is surely using get), if not take a look at Jon's answer.

也就是说,你不应该把它放在网址上。 ViewState是一个隐藏字段,通过回发(通常使用post)发送到服务器。它在页面呈现给客户端时保持控件的状态,并在每次回发时发送它。如果它适用于应用程序,你可以切换到使用post(问题表单肯定是使用get),如果没有看看Jon的答案。

Check this link for more information on how the viewstate fits into the asp.net life cycle: http://msdn.microsoft.com/en-us/library/ms972976.aspx.

有关viewstate如何适应asp.net生命周期的更多信息,请查看此链接:http://msdn.microsoft.com/en-us/library/ms972976.aspx。

#2


10  

I had a similar question when writing the Reputation Tracker.

在撰写信誉跟踪器时,我遇到了类似的问题。

I don't know how you do it globally other than to just never use a form with runat="server" set, which is more to do with discipline than a setting. In particular, if you have runat="server" set on a form I believe you'll always get a viewstate parameter, even if you've turned it off everywhere so you don't get any values. That was my experience, anyway.

我不知道你是如何在全局范围内做的,只是从不使用runat =“server”set的表单,这更多地与纪律而不是设置有关。特别是,如果您在表单上设置了runat =“server”,我相信您将始终获得一个viewstate参数,即使您已将其关闭,因此您不会获得任何值。无论如何,这是我的经验。

Obviously this limits you somewhat, but I've found that using the HTML server controls (instead of the ASP.NET controls) for appropriate parts of ASP.NET can make life a lot simpler to understand.

显然这对你有所限制,但我发现使用HTML服务器控件(而不是ASP.NET控件)来获得适当的ASP.NET部分可以让生活变得简单易懂。

#3


8  

Turn off the ViewState by default by using a <page> element in the web.config. Using EnableViewState="true" in the @Page directive will no longer work once you disable the ViewState in the web.config. If you decide later that you need the ViewState for a specific page, you can turn it back on for just that page using a <location> element.

默认情况下,使用web.config中的 元素关闭ViewState。一旦在web.config中禁用ViewState,在@Page指令中使用EnableViewState =“true”将不再有效。如果您稍后决定需要特定页面的ViewState,则可以使用 元素将其重新打开。

<configuration>
  <system.web>
    <pages enableViewState="false" />
  </system.web>

  <location path="MyFolder/MyPage.aspx">
    <system.web>
      <pages enableViewState="true" />
    </system.web>
  </location>
  <location path="Site.master">
    <system.web>
      <pages enableViewState="true" />
    </system.web>
  </location>
</configuration>

You need to do the same for any master pages that your ViewState enabled page uses.

您需要对启用ViewState的页面使用的任何母版页执行相同的操作。

#4


4  

Add this to the web.config file:

将其添加到web.config文件:

<Pages enableViewState="false"/> 

#5


3  

You could switch to ASP.Net MVC. From what I understand it doesn't use the ViewState.

您可以切换到ASP.Net MVC。据我所知,它不使用ViewState。

#6


2  

Do recall, however, that certain behaviors expected by most ASP.NET web forms developers will not work without ViewState. The purpose of ViewState is to provide the illusion that various page and control properties persist from one request to the next. ViewState does not contain all control properties, just those that have changed. The idea is that ViewState retains these properties as they were at the time the form last rendered.

但请记住,如果没有ViewState,大多数ASP.NET Web表单开发人员所期望的某些行为将无法运行。 ViewState的目的是提供各种页面和控件属性从一个请求到下一个请求持续存在的错觉。 ViewState不包含所有控件属性,只包含已更改的控件属性。我们的想法是,ViewState保留了上次呈现表单时的属性。

One good example is a SelectedIndexChanged event on a dropdown (one that does not have autopostback set). This works because ViewState retains the previous index, and the form posts the current index, and the control compares the two in order to know that the selected index has changed. That's when it raises the SelectedIndexChanged event. Without ViewState, that event will not fire. Same for TextChanged events, etc.

一个很好的例子是下拉列表上的SelectedIndexChanged事件(没有设置autopostback的事件)。这是因为ViewState保留了先前的索引,并且表单发布了当前索引,并且控件将两者进行比较以便知道所选索引已更改。那是它引发SelectedIndexChanged事件的时候。没有ViewState,该事件将不会触发。对于TextChanged事件等也是如此

Absent the GET situation (which I have never run into), the big problem with ViewState is using it where it's not needed. Your grid control does not need to retain the previous values of all controls in all its rows, so don't enable ViewState on it.

如果没有GET情况(我从未遇到过),ViewState的一个大问题是在不需要的地方使用它。您的网格控件不需要在其所有行中保留所有控件的先前值,因此不要在其上启用ViewState。

#1


34  

You can turn off viewstate for the whole site like this:

您可以关闭整个网站的viewstate,如下所示:

    <system.web>
<pages enableViewState="false" />

That said, you shouldn't be getting it on the url. ViewState is a hidden field that is sent to the server with a postback (which normally uses post). It keeps the state of the controls when the page was rendered to the client, sending it with each postback. If it works for the application you could switch to use post instead (the problem form is surely using get), if not take a look at Jon's answer.

也就是说,你不应该把它放在网址上。 ViewState是一个隐藏字段,通过回发(通常使用post)发送到服务器。它在页面呈现给客户端时保持控件的状态,并在每次回发时发送它。如果它适用于应用程序,你可以切换到使用post(问题表单肯定是使用get),如果没有看看Jon的答案。

Check this link for more information on how the viewstate fits into the asp.net life cycle: http://msdn.microsoft.com/en-us/library/ms972976.aspx.

有关viewstate如何适应asp.net生命周期的更多信息,请查看此链接:http://msdn.microsoft.com/en-us/library/ms972976.aspx。

#2


10  

I had a similar question when writing the Reputation Tracker.

在撰写信誉跟踪器时,我遇到了类似的问题。

I don't know how you do it globally other than to just never use a form with runat="server" set, which is more to do with discipline than a setting. In particular, if you have runat="server" set on a form I believe you'll always get a viewstate parameter, even if you've turned it off everywhere so you don't get any values. That was my experience, anyway.

我不知道你是如何在全局范围内做的,只是从不使用runat =“server”set的表单,这更多地与纪律而不是设置有关。特别是,如果您在表单上设置了runat =“server”,我相信您将始终获得一个viewstate参数,即使您已将其关闭,因此您不会获得任何值。无论如何,这是我的经验。

Obviously this limits you somewhat, but I've found that using the HTML server controls (instead of the ASP.NET controls) for appropriate parts of ASP.NET can make life a lot simpler to understand.

显然这对你有所限制,但我发现使用HTML服务器控件(而不是ASP.NET控件)来获得适当的ASP.NET部分可以让生活变得简单易懂。

#3


8  

Turn off the ViewState by default by using a <page> element in the web.config. Using EnableViewState="true" in the @Page directive will no longer work once you disable the ViewState in the web.config. If you decide later that you need the ViewState for a specific page, you can turn it back on for just that page using a <location> element.

默认情况下,使用web.config中的 元素关闭ViewState。一旦在web.config中禁用ViewState,在@Page指令中使用EnableViewState =“true”将不再有效。如果您稍后决定需要特定页面的ViewState,则可以使用 元素将其重新打开。

<configuration>
  <system.web>
    <pages enableViewState="false" />
  </system.web>

  <location path="MyFolder/MyPage.aspx">
    <system.web>
      <pages enableViewState="true" />
    </system.web>
  </location>
  <location path="Site.master">
    <system.web>
      <pages enableViewState="true" />
    </system.web>
  </location>
</configuration>

You need to do the same for any master pages that your ViewState enabled page uses.

您需要对启用ViewState的页面使用的任何母版页执行相同的操作。

#4


4  

Add this to the web.config file:

将其添加到web.config文件:

<Pages enableViewState="false"/> 

#5


3  

You could switch to ASP.Net MVC. From what I understand it doesn't use the ViewState.

您可以切换到ASP.Net MVC。据我所知,它不使用ViewState。

#6


2  

Do recall, however, that certain behaviors expected by most ASP.NET web forms developers will not work without ViewState. The purpose of ViewState is to provide the illusion that various page and control properties persist from one request to the next. ViewState does not contain all control properties, just those that have changed. The idea is that ViewState retains these properties as they were at the time the form last rendered.

但请记住,如果没有ViewState,大多数ASP.NET Web表单开发人员所期望的某些行为将无法运行。 ViewState的目的是提供各种页面和控件属性从一个请求到下一个请求持续存在的错觉。 ViewState不包含所有控件属性,只包含已更改的控件属性。我们的想法是,ViewState保留了上次呈现表单时的属性。

One good example is a SelectedIndexChanged event on a dropdown (one that does not have autopostback set). This works because ViewState retains the previous index, and the form posts the current index, and the control compares the two in order to know that the selected index has changed. That's when it raises the SelectedIndexChanged event. Without ViewState, that event will not fire. Same for TextChanged events, etc.

一个很好的例子是下拉列表上的SelectedIndexChanged事件(没有设置autopostback的事件)。这是因为ViewState保留了先前的索引,并且表单发布了当前索引,并且控件将两者进行比较以便知道所选索引已更改。那是它引发SelectedIndexChanged事件的时候。没有ViewState,该事件将不会触发。对于TextChanged事件等也是如此

Absent the GET situation (which I have never run into), the big problem with ViewState is using it where it's not needed. Your grid control does not need to retain the previous values of all controls in all its rows, so don't enable ViewState on it.

如果没有GET情况(我从未遇到过),ViewState的一个大问题是在不需要的地方使用它。您的网格控件不需要在其所有行中保留所有控件的先前值,因此不要在其上启用ViewState。