Url Routing ASP.NET WebPages (.cshtml) without MVC

2019-08-07 09:53发布

How can I route an url to a .cshtml page? E.g. www.example.com/view/2 --> view.cshtml

I don't want to use MVC and preferably not complicate things with controller code and other stuff. Just a routing as simple as possible.

1条回答
聊天终结者
2楼-- · 2019-08-07 10:04

Simple routes like the one where you just want to reach an existing file name without the extension work by default in a Web Pages site. The 2 is accessible in the UrlData collection.

Here's an article that explains how the rudimentary routing system works in Web Pages: http://www.mikesdotnetting.com/Article/165/WebMatrix-URLs-UrlData-and-Routing-for-SEO

If you want to map a url that doesn't match a file on disk, you can use a Nuget package (http://www.nuget.org/packages/WebPageRouteHandler/). It's a bit more complicated, but I have written an article about how to use that too: http://www.mikesdotnetting.com/Article/187/More-Flexible-Routing-For-ASP.NET-Web-Pages

查看更多
登录 后发表回答