user3929884
user3929884

Reputation: 213

Selfhosted MVC 5 Project

Hey there do you know how to run a MVC 5 Project without IIS or IIS Express local on your desktop ?

In ASP.NET vNext there is a WebListener which made that possible but I can't reorganice my project to ASP.NET vNext.

Is there any possibility running a MVC 5 Project as well as in ASP.NET vNext?

Upvotes: 0

Views: 765

Answers (2)

Tobias
Tobias

Reputation: 2840

Doesn't look like the other answers actually answer your question. The short answer is no, you cannot self host MVC 5, since it is dependent on IIS. If you want a self hosted web application, you either have to port your existing application to, for example, Nancy, or wait for the release of MVC 6 which can indeed be self hosted. Alternatively, you can look into Web Api, where the current version also can be self hosted.

Upvotes: 3

Oliver
Oliver

Reputation: 45109

Take a look at OWIN. It allows to instantiate your own little web server within your own application.

This is already available within the .Net framework through the WebApp.Start() method.

You simply create a class that contains a matching Configuration method that fills up the IAppBuilder and then you're done.

public class OwinStartup
{
    private static IDisposable _Server;

    public void Configuration(IAppBuilder appBuilder)
    {
        HttpConfiguration config = new HttpConfiguration();
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        config.Formatters.Insert(0, new JsonpFormatter());
        appBuilder.UseWebApi(config);
    }

    public static void Start()
    {
        // If the Start() method throws an exception, the problem
        // is a missing right. The url has to be registered somewhere
        // deep down in windows and this is only allowed by the admin.
        // But you can change this rule to allow this registration for
        // anybody by running the below command within a command prompt
        // with admin rights:
        // netsh http add urlacl url=http://+:14251/ user=Everyone
        // Depending on your OS language the group name can differ.

        string baseAddress = "http://+:26575/";

        try
        {
            _Server = WebApp.Start<OwinStartup>(url: baseAddress);
        }
        catch (HttpListenerException ex)
        {
            _Log.Message(Severity.Fatal, "Could not start web api listener.", ex);
            _Log.Message(Severity.Notice, "This normally happens cause the application is not allowed to add a web listener.");
            _Log.Message(Severity.Debug, "Open up a command prompt with admin rights and execute the following command: \"netsh http add urlacl url="+ baseAddress +" user=Everyone\"");
        }
    }

    public static void Stop()
    {
        if(_Server != null)
        {
            _Server.Dispose();
            _Server = null;
        }
    }
}

Upvotes: 0

Related Questions