European ASP.NET MVC 4 and MVC 5 Hosting

BLOG about ASP.NET MVC 3, ASP.NET MVC 4, and ASP.NET MVC 5 Hosting and Its Technology - Dedicated to European Windows Hosting Customer

European ASP.NET MVC 5 Hosting - UK :: How to Fix Error Could not load file or assembly 'Microsoft.Web.Infrastructure'

clock September 24, 2014 07:39 by author Scott

We believe that some of you get this error when deploying your ASP.NET MVC 5 to shared hosting:

[FileNotFoundException: Could not load file or assembly 'Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.]

Previously, we have written blog about how to deploy your MVC to shared hosting environment. In this tutorial, we will be more focusing on above error message.

This is also one of a few component libraries that are needed for deploying an MVC application:

  • System.Web.Helpers.dll (required by the web.config)
  • System.Web.Mvc.dll
  • System.Web.Razor.dll
  • System.Web.WebPages.dll
  • System.Web.WebPages.Razor.dll
  • Microsoft.Web.Infrastructure.dll

The system libraries are installed with .NET 4, however, 'Microsoft.Web.Infrastructure.dll' is only installed when Visual Studio is installed on the machine.  Therefore, short of needing to install MVC and Visual Studio on a production environment, we need to deploy the libraries with out application - and we'd like to do so automatically.

There are a few ways to automatically deploy the 'Microsoft.Web.Infrastructure.dll' component library with your application.  The steps depend on which version of Visual Studio you are using.

1. Right-click on your project and select "Add Deployable Assemblies" and you'll see the following dialog:

2. When deploying for MVC, only choose the first option.  Never mind the second option even though it says "Razor syntax." The second option is for deploying the required libraries for projects officially known today as ASP.NET Web Pages. Once you click OK, you'll see a new folder appear in your project called _bin_deployableAssemblies with the required libraries.

For .NET, this is a special folder:

  • This is a secondary bin folder for framework dependencies.  If you right-click on any one of the .dll's shown in this folder, you'll see that the libraries are set to "Copy to Output Directory".  When the application is packaged/deployed, the libraries (and the folder) are also copied.  Again, the framework will also automatically check this folder for dependencies.
  • For .NET, any folder that begins with an underscore ("_") is considered private and non-accessible to the browser.  Therefore, you can rest knowing that the dependencies are secure.

Note that this process did not add any references to these libraries in your project.  They are simply here for the framework to run your MVC application.  If you do, in fact, need a type or class from one of these libraries, you are free to still add it as a reference as you normally would.

The above method is for Visual Studio 2010. How about in Visual Studio 2012? Then, please stay here, don’t exit from our blog. We almost finish our tutorial. Stay focus. :)

After Visual Studio 2010, the "Add Deployable Assemblies" option was removed from the project's options menu.  The system libraries are automatically copied to the Bin folder of your project. However, again, the 'Microsoft.Web.Infrastructure.dll' is only available on machines that have Visual Studio installed.  So how do you deploy this library with you application if you don't want to install Visual Studio on a production environment?  I'm glad you asked.  You'll need to use the Package Manager.

1. Run the following command in the package manager (if you're not familiar with Visual Studio, this can be reached by going to "Tools --> Library Package Manager --> Package Manager Console" in the top menu).

2. At the PM> prompt type

Install-Package Microsoft.Web.Infrastructure

3. You will see then see a message indicating that it has been installed and added to your project.

4. You'll also see that 'Microsoft.Web.Infrastructure.dll' has been added as a reference in your References folder.

5. Finally, now, when you deploy your application, 'Microsoft.Web.Infrastructure.dll' will be included in the Bin folder.

Conclusion

We hope with tutorial above, you can easily deploy your MVC application without any problem. If this article, please share it!! Please tell the world. :)



European ASP.NET MVC 5 Hosting - Amsterdam :: New Filter Overrides Feature in ASP.NET MVC 5

clock December 30, 2013 07:07 by author Scott

In this quick article, I will tell you the problem that filter overrides is trying to solve(with an example). I will also show you a quick fix of a bug that I found in ASP.NET MVC 5 regarding filter overrides.

Description:

Let say you have an Authorize filter applied on a controller and a action

[Authorize(Roles = "PowerUsers")]
public class AController : ApiController
{   

    // GET api/api
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }
    [Authorize(Roles = "Admin")]
    // GET api/api/5
    public string Get(int id)
    {
        return "value";
    }
}

Now you want that all actions of above controller should only run when the user is in PowerUsers role except for second Get method which requires an Admin role. Before ASP.NET MVC 5 and ASP.NET Web API 2, it was not possible to achieve this(without using a dirty hack). But the current version make this a piece of cake, using OverrideAuthorizationAttribute. Just put this attribute on the second Get action above and then you are done. For the further understanding of this, just use this controller and custom authorize filter,

public class MyAuthorizeAttribute : AuthorizeAttribute
   {
       string name;
       public MyAuthorizeAttribute(string s)
       {
           name = s;
       }
       public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext)
       {
           base.OnAuthorization(actionContext);
       }
   }
   [MyAuthorize("Controller")]
   public class AController : ApiController
   {
       [MyAuthorize("Action")]
       //[OverrideAuthorization]
       // GET api/api
       public IEnumerable<string> Get()
       {
           return new string[] { "value1", "value2" };
       }
       // GET api/api/5
       public string Get(int id)
       {
           return "value";
       }
   }

Now just put a break-point on MyAuthorizeAttribute constructor and OnAuthorization method. You will find that the constructor is invoked for both action and controller but only the controller's MyAuthorizeAttribute.OnAuthorization(by checking name variable) is called. Now just un-comment the[OverrideAuthorization], you will find that action method's MyAuthorizeAttribute.OnAuthorization is called. When you do the same in MVC 5, it will not work due to a bug(AFAIK) in MVC 5 RC. Hopefully will be fixed in RTM. But for now you can use this hack to make this work in MVC 5 as well. Here is an example,

public class OverrideAuthorizeAttribute : AuthorizeAttribute, IOverrideFilter
   {
       public Type FiltersToOverride
       {
           get { return typeof(IAuthorizationFilter); }
       }
   }
   [Authorize(Users="Admin")]
   public class HomeController : Controller
   {
       [OverrideAuthorize(Users = "Imran Baloch")]
       public ActionResult Index()
       {
           return View();
       }       

       public ActionResult About()
       {
           ViewBag.Message = "Your application description page.";
           return View();
       }

       public ActionResult Contact()
       {
           ViewBag.Message = "Your contact page.";

           return View();
       }
   }

The hack is simply creating a new OverrideAuthorizeAttribute inheriting from AuthorizeAttribute and implementing IOverrideFilter interface and then applying this to the Index action method.

 



About HostForLIFE.eu

HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.

We have offered the latest Windows 2016 Hosting, ASP.NET Core 2.2.1 Hosting, ASP.NET MVC 6 Hosting and SQL 2017 Hosting.


Tag cloud

Sign in