Do I need a Global.asax.cs file at all if I'm using an OWIN Startup.cs class and move all configuration there?

后端 未结 3 2106
北荒
北荒 2020-11-27 09:58

Let\'s say for example in a brand new ASP.NET MVC 5 application made from the MVC with Individual Accounts template, if I delete the Global.asax.cs class and mo

3条回答
  •  情话喂你
    2020-11-27 10:22

    For those looking for the complete steps: If you are looking to create a OWIN based, IIS hosted web API, these steps should get you there:

    1. File -> New -> Project
    2. In the dialogue, Installed -> templates -> Other Project types -> Visual Studio Solutions -> Blank Solution targeting .NET 4.6
    3. On the solution, right click, add Project -> Web -> ASP.NET Web Application (targeting .NET 4.6)

      3.1 Now In the ASP.NET 4.5 templates, choose Empty as the template

      3.2 This creates a blank solution with two nuget packages:

      Microsoft.CodeDom.Providers.DotNetCompilerPlatform v 1.0.0
      Microsoft.Net.Compilers v 1.0.0
      
    4. Install the following packages:

      Install-Package Microsoft.AspNet.WebApi.WebHost -Version 5.2.3
      Install-Package Microsoft.AspNet.WebApi -Version 5.2.3
      Install-Package WebApiContrib.Formatting.Razor 2.3.0.0
      

    For OWIN:

    Install-Package Microsoft.Owin.Host.SystemWeb 
    Install-Package Microsoft.AspNet.WebApi.OwinSelfHost    
    

    Then add Startup.cs with Configuration method:

    [assembly:OwinStartup(typeof(namespace.Startup))]
    public class Startup
        {
            ///  Configurations the specified application. 
            /// The application.
            public static void Configuration(IAppBuilder app)
            {
                var httpConfiguration = CreateHttpConfiguration();
    
                app
                    .UseWebApi(httpConfiguration);
            }
    
            ///  Creates the HTTP configuration. 
            ///  An  to bootstrap the hosted API 
            public static HttpConfiguration CreateHttpConfiguration()
            {
                var httpConfiguration = new HttpConfiguration();
                httpConfiguration.MapHttpAttributeRoutes();
    
                return httpConfiguration;
            }
    }
    

    Now add a class that inherits from ApiController, annotate it with RoutePrefix attribute and the action method with Route + HttpGet/PutPost (representing the Http verb you're after) and you should be good to go

提交回复
热议问题