C#ASP.NET页内代码没有告诉我“响应”不存在

时间:2011-02-24 10:03:04

标签: c# .net asp.net cookies

我在运行的umbraco中有一些代码并创建一个javascript文件,用于查找他们的IP并将它们发送到正确的站点以获取他们的语言,现在这样可行,但我正在尝试添加一个覆盖系统,所以a网站的用户可以使用cookie强制自己退出重定向,但出于某种原因,当我尝试编写响应时,它告诉我在设置之前不能使用Response局部变量

<%@ Master Language="C#" MasterPageFile="/umbraco/masterpages/default.master" AutoEventWireup="true" Debug="true" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>

<asp:Content ContentPlaceHolderID="ContentPlaceHolderDefault" runat="server">
 <script runat="server">
   string getOutput(){
     HttpCookieCollection MyCookieColl;
     HttpCookie MyCookie;

     MyCookieColl = Request.Cookies;
     MyCookie = MyCookieColl["override"];
     try{
       if(MyCookieColl["override"].Value == "overridden"){
        HttpCookie cookie = new HttpCookie("override");
        cookie.Value = "overridden" ;
        DateTime dtNow = DateTime.Now;
        TimeSpan tsMinute = new TimeSpan(0, 0, 10, 0);
        cookie.Expires = dtNow + tsMinute;
        Response.Cookies.Add(cookie);
        return "";
      }
     }catch(Exception e){
     }

     try{
     if(Request.Form["overide"] == "overridden"){
       HttpCookie cookie = new HttpCookie("override");
        cookie.Value = "overridden" ;
        DateTime dtNow = DateTime.Now;
        TimeSpan tsMinute = new TimeSpan(0, 0, 10, 0);
        cookie.Expires = dtNow + tsMinute;
        Response.Cookies.Add(cookie);
       return "";
       }
     }catch(Exception e){
     }
   [[[TRIMMED CODE NOT NEED BELOW WORKS]]]]
 </script>
 <%=getOutput()%>
</asp:Content>

1 个答案:

答案 0 :(得分:4)

您是否尝试过使用HttpContext.Current.Response?

使用开箱即用的"Response"可能需要导入指令。