How to force all requests in ASP.NET 5 MVC project to redirect to root "/"? -


this post stephen walther talks redirecting requests in mvc project modifying web.config system.webserver / rewrite section.

http://stephenwalther.com/archive/2015/01/16/asp-net-5-and-angularjs-part-3-adding-client-routing

however, seems wrong have reintroduce web.config xml file asp.net 5 project.

is there way in asp.net 5? maybe via new config.json?

in startup's configure method, app iapplicationbuilder:

app.run(context => {     context.response.redirect("/");     return task.fromresult<object>(null); }); 

this send otherwise unhandled requests root of application. place last, after usestaticfiles() or other middleware registrations.

note not capture registrations above this; if have other routes on server (such controller actions, etc.), not captured. should work added benefit don't need exclude patterns such in example.

alternatively...

if you're doing single page application, want allow deep linking users. use simple attribute routing that:

[route("", order = -1)] [route("{*pathinfo}", order = 1000)] public async task<iactionresult> index(string pathinfo = "", cancellationtoken cancellationtoken = default(cancellationtoken)) {         return view("uiview"); } 

this map default requests (/) priority while mapping other requests (allowing default ordering, etc. take priority) "uiview".

if don't want use attribute routing, use method above following route mapping:

// before routes routebuilder.maproute(     "root",     "",     defaults: new { controller = "home", action = "index" });  // routes here  // after routes routebuilder.maproute(     "deeplink",     "{*pathinfo}",     defaults: new { controller = "home", action = "index" }); 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -