Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top