Before I ask my question I have already gone through the following posts:
Here is my project's folder layout:
Currently there is no controller or view. Just the Owin Startup
file.
Startup.cs
using System;
using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(Bootstrapper.Startup))]
namespace Bootstrapper
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.Run(async context =>
{
await context.Response.WriteAsync(GetTime() + " My First OWIN App");
});
}
string GetTime()
{
return DateTime.Now.Millisecond.ToString();
}
}
}
Web.config
<appSettings>
<add key="owin:AutomaticAppStartup" value="true" />
<add key="owin:appStartup" value="Bootstrapper.Startup" />
<add key="webpages:Version" value="2.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
I have the following reference in the Bootstrapper
project:
UPDATE:
Forgot to add the error message:
Now,
Owin
Startup
class in a very basic project(like accessing Home/Index
)?Owin Startup
class is
called/executed?
UPDATE: on 10-Dec-2016
Check the Project-Folder-Layout
. In Bootstrapper
project I have the following file:
IocConfig.cs
[assembly: PreApplicationStartMethod(typeof(IocConfig), "RegisterDependencies")]
namespace Bootstrapper
{
public class IocConfig
{
public static void RegisterDependencies()
{
var builder = new ContainerBuilder();
builder.RegisterControllers(typeof(MvcApplication).Assembly);
builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());
builder.RegisterModule<AutofacWebTypesModule>();
builder.RegisterType(typeof(MovieService)).As(typeof(IMovieService)).InstancePerRequest();
builder.RegisterType(typeof(MovieRepository)).As(typeof(IMovieRepository)).InstancePerRequest();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
}
}
Now I want to execute IocConfig.RegisterDependencies()
in OWIN Startup
class. I am doing using Bootstrapper
in Startup
at the top but, it is not working. I mean I am unable to reference IocConfig
in Startup
. How to resolve this?
install-package Microsoft.Owin.Host.SystemWeb
)Here I will answer your third question. The startup class is an entry point of OWIN and is being looked up automatically. As stated in official docs:
Naming Convention: Katana looks for a class named Startup in namespace matching the assembly name or the global namespace.
Note, that you can also choose your own name of Startup class but you have to set this up using decorators or AppConfig. As stated here: https://www.asp.net/aspnet/overview/owin-and-katana/owin-startup-class-detection
This is everything you need for a basic and working OWIN test:
using Owin;
using System;
namespace OwinTest
{
public class Startup
{
public static void Configuration(IAppBuilder app)
{
app.Use(async (ctx, next) =>
{
await ctx.Response.WriteAsync(DateTime.Now.ToString() + " My First OWIN App");
});
}
}
}
If you wish to use MVC (I guess by "Home/Index" you mean MVC), follow these steps:
install-package Microsoft.AspNet.Mvc
).Make the page inherit from WebViewPage. It should all look like this:
@inherits System.Web.Mvc.WebViewPage
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<h1>Owin Hello</h1>
</div>
</body>
</html>
global.asax
to set up routes. Right click on the project -> add -> New Item -> Global Application Class.Add the routes definition to the Application_Start method:
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapRoute(name: "Default",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index" });
}