SeanMcCammon C# .Net core 3 Software Developer

If you’re anything like me: when you wanted to find out more about ASP.NET you went to the internet.

May be you went to an online site that has training courses such as Udemy. Maybe you searched and found some tutorial pages. 

Me personally, I did both and after trying to follow some of these I found information that had gone out of date.

starting wit information I leant for Route

When I was trying to add a custom route to a page/method - I followed a course which told me to add it to RouteConfig.cs.

Configure the new route for my custom page and inputs in using MapRoute().

With a default .Net Core MVC build in Visual Studio 2019 this file was not in the project.

Examining the default C# files; in Startup.cs the endpoints are mapped using MapControllerRoute().

This worked well but did not give me the flexibility I was after. 

What I really wanted was to have a custom route defined for specific methods in my class that managed a single page.

For example: if I have a page called Movie. I wanted a page that maybe called Random. I then may want a page that displays Movie Titles or Authors.

Using the MapControllerRoute was starting to get a little clunky (using a technical term there) and did not give me the flexibility I needed. So, I decided to use Route in the controller.

Custom routes in the controller

Using this method of adding custom routes allows you to add a route per method for you controller.

So, as an example

Route(“Movie/Author/{name}”)]
Public IActionResult ByAuthor(string name) {...} </code></p>

The above allows us to go to a page named Http://our_site/Movie/Random

And also a page where we pass in a parameter Http://out_site/Movie/Author/Sean

In the above, Sean is passed is as a parameter and goes into the name parameter. We can then search our list of movies for the author name and output content found.

Both these methods would exist in the controller - which maybe named MovieController.cs

The best way to route in ASP.NET code

This, in my opinion, is the best way too route to pages in your ASP.NET code. Each method has its own custom route defined which includes any parameters. 

You don’t need to add any code for the route into your startup.cs file; as it’s all contained in the controller file.

I wanted to start this post with a quick caveat. At the time of writing this post: the .Net Core3 is currently at Preview 8.

As we grow closer to the September release date I hope that many of the code and feature I talk about in my upcoming blog posts will remain the same.

However, I may have to come back to some of them in the future to fine tune.

That said, let us begin

Why write about .Net Core 3?

So, if you look at my previous posts on this site: most are technical. None of them really go into too many specifics apart from any coding examples.

I’m now on a journey myself: a journey of learning about .Net Core 3. A journey that is a discovery of all the new features - but also older features that have not changed.

A journey that will see me work with and code new products. A journey that will also see me learning more about ASP.NET as this is something I have not used commercially.

So, as I travel along this learning journey I thought I would share with you all the things I learn. 

The initial posts maybe more suited to people who are starting to learn C# or .Net Core 2/3 - but I hope to get more technical as the posts go on.

So who maybe interested in these posts?

As I said above. My original though is, that, many of the people interested in these posts will be those on a learning journey.

However, it may be that, you may who already has knowledge of .Net Core 2 and want to see what has changed: see what has changed from my perspective and journey.

These are the main persons who I would think this blog of interest. Though I expect it could be useful to other groups. Whoever is reading this post and any others: welcome to my blog and I hope to interact with you.

More posts to come

So, this is really just an introduction to this new series of posts. I wanted to just say hi and detail a little of what I expect to write.

I do have more posts to write. Posts that will start to delve into a little more detail: such at [Route] for custom routes above methods.

So if you think this is something you will enjoy or find useful for you to read; keep popping back for the latest updates.

See you soon in the next and first post of this series.

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram