Using Server in Mvc.Controller

2019-02-25 06:55发布

I have my own inherited App.Controller from Mvc.Controller which then all of my controllers inherit from. I wrote a provider utilizing an interface and implemented it as MyService and the constructor takes the Server property of Mvc.Controller which is of HttpServerUtilityBase.

However, I instantiate MyService in App.Controller's constructor. The problem is that the Server property of the Controller is null when constructing MyService. I have used public Controller () : base() { } to get the base to be constructed. However, Server remains null.

I would like to avoid Web.HttpContext.Current.Server if possible.

Has any one have a work around for this problem?

Edit: Well, I have implemented tvanfosson's suggestion, and when my app constructs MyService in the property get method, Server is still null.

Edit 2: Nevermind, I was a goof. I had another Controller using Server aswell and did not change that. Case closed.

5条回答
▲ chillily
2楼-- · 2019-02-25 07:23

I instantiate MyService in App.Controller's constructor.

There's your problem. You need to pass an instance of MyService which has already been constructed into your App.Controller's constructor. Take a look at the Inversion of Control / Dependency Injection patterns, and take a look at some of the libraries which make these patterns easy (see this list).

查看更多
Animai°情兽
3楼-- · 2019-02-25 07:25

Use delayed initialization to construct your service.

private MyService service;
public MyService Service
{
    get
    {
         if (this.service == null)
         {
             this.service = new MyService(this.Server);
         }
         return this.service;
    }
}

Then, your service isn't actually instantiated until it is used in the controller action and by that time the Server property has been set.

查看更多
smile是对你的礼貌
4楼-- · 2019-02-25 07:39

Why do you need the Server reference? Are you doing stuff like url/html encoding? If so, you could use HttpUtility instead and get rid of the context reference entirely.

查看更多
Evening l夕情丶
5楼-- · 2019-02-25 07:41

This is a very old question, but the subject is still relevant. So, one hint in 2017 that was possibly not available in 2009:

It is true that in the Controller constructor Server is null. But you can use the OnActionExecuting event:

protected override void OnActionExecuting(ActionExecutingContext filterContext) 
{
    base.OnActionExecuting(filterContext);

    CurrentServer = Server; // CurrentServer is some instance variable I need later.
} 

This works fine for me.

查看更多
在下西门庆
6楼-- · 2019-02-25 07:42

According to this site, if you have this Controller:

public class MyController : Controller
{
  private string folderPath;

  public MyController()
  {       
    // Throws an error because Server is null
    folderPath = Server.MapPath("~/uploads"); 

    // Throws an error because this.ControllerContext is null
    folderPath = this.ControllerContext.HttpContext.Server.MapPath("~/uploads"); 
  }
}

Then you want to initialize it this way:

protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
  base.Initialize(requestContext);

  // now Server has been initialized
  folderPath = Server.MapPath("~/uploads"); 
}
查看更多
登录 后发表回答