在asp.net mvc上的Page.Request?

时间:2022-10-07 17:00:32

I was trying to execute this code, except the page_load

我试图执行此代码,除了page_load

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

in a controller

在控制器中

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

However I get this error

但是我收到了这个错误

Error 1 An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Request.get'

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

How can I get the current page request in MVC?

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

Update 1:

I changed to:

我改为:

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

Now I got:

现在我得到了:

Error 2 'System.Web.HttpContextBase' does not contain a definition for 'Current' and no extension method 'Current' accepting a first argument of type 'System.Web.HttpContextBase' could be found (are you missing a using directive or an assembly reference?)

错误2'System.Web.HttpContextBase'不包含'Current'的定义,并且没有扩展方法'Current'可以找到接受类型'System.Web.HttpContextBase'的第一个参数(你是否缺少using指令或者装配参考?)

What assembly do I need?

我需要什么组件?

These are my usings

这些是我的用途

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

1 个解决方案

#1


5  

You don't have a Page in ASP.NET MVC, this is an ASP.NET Web Forms concept (inheritance from Page). When you are working within MVC, you are in the scope of a Controller class which contains a reference to the current Context.

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

Instead, in MVC, use: HttpContext.Request, like that:

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

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

#1


5  

You don't have a Page in ASP.NET MVC, this is an ASP.NET Web Forms concept (inheritance from Page). When you are working within MVC, you are in the scope of a Controller class which contains a reference to the current Context.

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

Instead, in MVC, use: HttpContext.Request, like that:

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

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