如果Request在System.Web.Mav命名空间内,在Controller类中(参见下面的F12自动生成结构), 为什么我无法用完全限定名称
来引用它System.Web.Mvc.Controller.Request.Browser`
namespace System.Web.Mvc
{
public abstract class Controller : ControllerBase, IActionFilter, IAuthorizationFilter, IDisposable, IExceptionFilter, IResultFilter, IAsyncController, IController, IAsyncManagerContainer
{
protected Controller();
....
public HttpRequestBase Request { get; }
....
}
}
我正在使用Microsoft Visual Studio Ultimate 2013和C# 谢谢!
答案 0 :(得分:2)
这是因为此Request
属性是Controller
的实例属性,而不是静态属性。
public class MyController : Controller
{
public ActionResult MyAction()
{
//you can access this.Request.Browser
//because "this" gives an instance of Controller
//you can't use System.Web.Mvc.Controller.Request.Browser
//because "Request" is not a static property of "Controller"
}
}
为简化起见,请考虑以下事项:
class Person
{
public string Name { get; }
}
//Person.Name -> invalid, "Name" property is not static
//new Person().Name -> valid, "Name" property is access via an instance of "Person"