【问题标题】:Page.Request on asp.net mvc?在asp.net mvc上的Page.Request?
【发布时间】:2014-03-07 04:41:10
【问题描述】:

我试图执行这段代码,除了 page_load

protected void Page_Load(object sender, EventArgs e) 
{ 
    var contextToken = TokenHelper.GetContextTokenFromRequest(Page.Request); 

在控制器中

var contextToken = TokenHelper.GetContextTokenFromRequest(Page.Request);

但是我得到了这个错误

错误 1 ​​非静态字段、方法或属性“System.Web.UI.Page.Request.get”需要对象引用

如何在 MVC 中获取当前页面请求?

更新 1:

我改成:

 var contextToken = TokenHelper.GetContextTokenFromRequest(HttpContext.Current.Request);

现在我得到了:

错误 2 'System.Web.HttpContextBase' 不包含对 'Current' 并且没有扩展方法 'Current' 接受第一个参数 可以找到“System.Web.HttpContextBase”类型的(您是否缺少 使用指令还是程序集引用?)

我需要什么组件?

这些是我的用途

using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;

【问题讨论】:

  • 摆脱.Current,只使用TokenHelper.GetContextTokenFromRequest(HttpContext.Request)

标签: c# asp.net asp.net-mvc


【解决方案1】:

您在 ASP.NET MVC 中没有 Page,这是一个 ASP.NET Web 窗体概念(继承自 Page)。在 MVC 中工作时,您处于 Controller 类的范围内,该类包含对当前 Context 的引用。

相反,在 MVC 中,使用:HttpContext.Request,就像这样:

var contextToken = TokenHelper.GetContextTokenFromRequest(HttpContext.Request);

【讨论】:

  • 说句公道话,MVC 是建立在 ASP.NET 之上的,页面更像是一个 WebForms 概念。 (请事实核查)
  • 这是真的,这就是为什么你在 MVC 中有 HttpContext.Current.Request
  • @gunr217122:你是对的。它现在称为 ASP.NET Web 窗体。
  • 我的参考文献有误吗??错误 2“System.Web.HttpContextBase”不包含“Current”的定义,并且找不到接受“System.Web.HttpContextBase”类型的第一个参数的扩展方法“Current”(您是否缺少 using 指令或程序集参考?)
  • @L.V.SharepointArchitect,使用您拥有的任何信息编辑您的帖子。在 cmets 中更难阅读。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-07
  • 2010-10-01
  • 2010-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-23
相关资源
最近更新 更多