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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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;
        }
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x