ASP.NET MVC url construction - asp.net

Before I ask my question: I just started ASP.NET MVC, so an advanced answer will maybe hard to understand ;)
I'm having 3 tables (I'll add just a few of the fields below)
Shops (ShopID, Name)
Products (ProductID, Name, ShopID)
Comments (CommentID, ProductID, UserID, CommentText)
Now what I want to do is this:
www.site.com/Shops
//Gives me a list of shops
www.site.com/Shops/ShopName
//Gives me some details about the shop called ShopName and lists all the products that the shop has
www.site.com/Shops/ShopName/ProductName/
//Gives me all the comments of that product called ProductName from that specific shop called ShopName
www.site.com/Shops/ShopName/ProductName/ASpecificCommentHere
//Gives me a specific comment on that product of that shop
and other things like
www.site.com/Shops/ShopName/ProductName/AddComment
//Adds a new comment to that product from that shop
Now I only have a ShopsController which makes it possible for me to do something like this now:
www.site.com/Shops/
// Gives me a list of all shops
and
www.site.com/Shops/Details/123
// Gives me some details about the shop with ID 123 and should list its products.
But here is also the first problem: I don't want the ID number in the url like /Shops/123 but rather the name like /Shops/ShopName and I don't know if it's good to have /Details/ there in the url. Maybe /Shops/ShopName would be better without the Details part in between?
How should I do this?
Should I create also a Controller for my Products and for my Comments that I have 3 controllers in total?
Or should I just keep one ShopController to always get the first part of the url (so that it always starts with) site.com/Shops/ShopName/...
Thanks in advance
(and a short little question, should ViewModels be placed in the controllers directory?)

Kind a doing your work, but - whatever.
I would go with something like this (be warned - it's untested)...
As you already likely know - order is important.
www.site.com/Shops/ShopName/ProductName/AddComment =>
routes.MapRoute(
"Product",
"Shops/{shopName}/{productName}/{action}",
new { controller="comment"}
);
www.site.com/Shops/ShopName/ProductName/ASpecificCommentHere =>
routes.MapRoute(
"Product",
"Shops/{shopName}/{productName}/{commentName}",
new { controller="comment", action="details"}
);
www.site.com/Shops/ShopName/ProductName/AddProduct =>
routes.MapRoute(
"Product",
"Shops/{shopName}/{productName}/{action}",
new { controller="product"}
);
www.site.com/Shops/ShopName/ProductName/ =>
routes.MapRoute(
"Product",
"Shops/{shopName}/{productName}",
new { controller="product", action = "details"}
);
www.site.com/Shops/ShopName =>
routes.MapRoute(
"ShopDetails",
"Shops/{shopName}",
new { controller="shop", action = "details"}
);
www.site.com/Shops (NB: this route is 'global', for every controller) =>
routes.MapRoute(
"List",
"{controller}",
new { action = "list"}
);
Maybe this can be optimized or worse - it's wrong but it should give you some better understanding for sure.
Remember not to name shops/controllers/comments as according controller actions. That's an issue with this routing approach.
And i hope you noticed that i would create 3 different controllers (what might and might not be a good decision - that's based on expectable weight and complexity of controller logic).
Another tip - read about model binding. Using custom binders you could replace string parameters (entity names) with actual, already assembled objects. Maybe that wouldn't be worth for this particular case, but model binding surely is 2nd important and hardest thing to grasp when facing asp.net mvc.
And i lied, i wouldn't go with this because i prefer specifying controller specific routes using attributes directly on actions (omitted that because it seems more advanced).
Routing is like regular expressions. There's a moment when it just 'clicks' and seems natural and easy.
Already answered second question as comment to your question. :)

I would spend a little time researching the routing mechanism in MVC. This is where you set the formats for the URLs your application should receive. All the things you mentioned are possible...you just need to construct your route definitions accordingly. As far as being able to accept the name of the entity, rather than the ID, you can do that very easily by constructing your route so that it accepts names and changing the signatures on your action methods to accept strings instead of ints.
I don't know what the official stance on where ViewModels should be located. It's really up to you, and it depends on the ORM method you are implementing. I'm using LINQ-to-SQL, and having a very good time with it. I keep my LINQ-to-SQL model in my Models folder. I prefer to keep my ViewModels in a View subfolder under my Models folder. I've also got a Domain subfolder to hold the partial models for my entities, in case I want to implement calculated fields or set up the initial values for the record.

Related

How to approach making a controller with (unlimited) db look-up routes

I'm digging into ASP.NET MVC from classic asp, and have completed some tutorials. I understand the concept now, but I have a main question about the controller. How are you able to control the url structure if you are getting your url's (with params) from a sql database?
Example: /custom-url-1 or /custom-url-23423411
(Returns params accordingly to feed the code)
I'm guessing it would have to do with ActionResult Index() , but not sure where to go after that. Any idea's where to look or is this even possible? Does MVC even allow this?
One Way you can approach this is to have everything go to one action in one controller and resolve the content in the view.
This is useful only if you have one type of view.
Second way is to have route constraint or a custom route constraint for each type of content you have
say : Galleries, Blog, Pages
and in each constraint check if the given url is of this type ( by db call), if the constraint returns true, it will point the request to the given controller and action.
The third way is to have a custom route handler that does the checking and routing (mind you this is probably the hardest task but works best if you have complex system, if your is simple try using method 1 or 2
P.S. if you want your urls separataed by "-" instead of "/" you can do just this
routes.MapRoute(
"Default", // Route name
"{controller}-{action}-{id}",// URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);

Handling Singular and Plural Controllers/Routes

I'm a little confused on how I should handle singular and plural routes and controllers in my web application.
The website is a simple quotes site - think Einstein, Shakespeare etc. not Insurance. Within the project I have a controller called `QuoteController'. The controller name is singular, so does this mean that the controller should only handle the display of single quotes? I.E.
/quote/love-is-a-battlefield-1
Do I then need another controller for the display of multiple quotes (plural)? For example:
/quotes/ (would default to most recent)
/quotes/new
/quotes/shakespeare
/quotes/popular
Is it convention, or good practice, to have separate controllers for singular and plural routes? I hope that makes sense.
Just because the default controllers of asp-mvc have singular names, it doesn't mean you should implement singular form for all your controllers.
The correct answer is: it depends on the quantity of the entity that your controller represents.
Singular, example the AccountController is singular because it represents actions (action method) pertaining to a single account only.
Plural If your controller contains at least one action method that handles multiple entities at a single transaction.
Example plural format
users/update/3
The route above makes you think you are editing all users, which does not makes sense if you read it like a sentence. However, if you read your route like query, it will make much more sense.
If we think about it, a route IS a query: {entities}/{action}/{parameter} looks like a query to me.
users/ shorthand of users/all reads "select all users table"
users/123 reads "select single entity from users table"
users/update/123 reads "update single entity from users table"
Major sites use plural format, see sample below
stackoverflow.com/questions <- list of questions (multiple)
stackoverflow.com/questions/18570158 <- individual question (single)
stackoverflow.com/questions/ask <- new question (single)
stackoverflow.com/users <- display list of users (multple)
stackoverflow.com/users/114403 <- individual user (single)
asp.net/mvc/tutorials <- display list of tutorials (multiple)
asp.net/mvc/tutorials/mvc-5 <- individual tutorial (single)
facebook.com/messages/ <- Display list of messages (multiple)
facebook.com/messages/new <- Create a single message (single)
facebook.com/messages/john <- view individual messages (multiple)
I believe that English grammar should be strictly incorporated in every programming aspect. It reads more natural, and leads to good code hygiene.
Answer
Here's a question asked in programmers StackExchange that suggests keeping class names singular. I especially like the logic in one of the answers, "The tool to turn screws with is called "screw driver" not "screws driver"." Which I agree with. The names should be kept to nouns and adjectives.
As far as routing goes, best practices seems to favor that routes be pluralized nouns, and to avoid using verbs. This Apigee blog says, "avoid a mixed model in which you use singular for some resources, plural for others. Being consistent allows developers to predict and guess the method calls as they learn to work with your API." and suggests using singular or plural based off what popular websites have done. Their only example for using singular nouns in the route is the Zappos site, which has http://www.zappos.com/Product as a route; but if you examine the site, it's not exactly a route to a product resource but is instead a query, probably for all their products. You can enter http://www.zappos.com/anything and get a results page. So I wouldn't put too much stock into that.
Other blogs like this one from mwaysolutions (random find) say to "use nouns but no verbs" and "use plural nouns". Other points aside, most blogs/articles tend to say the same thing. Plural nouns and no verbs.
TL;DR: Use singular nouns for controllers and plural nouns for routes.
Controllers
Controllers and routes represent two different concepts. The controller is a class, or a blueprint. Similar to a stamper, I wouldn't say I had a UnicornsStamper I'd say I had a UnicornStamper that makes a stamp of a unicorn with which I could make a collection of unicorn stamps with. Collections, Enum types that are bit fields, static class that is a collection of properties (acts like a collection), and probably a few other edge cases are where I'd use a plural name.
Routes
A(n) URL is an address to a resource, thus the name Uniform Resource Locator. I disagree that "a route IS a query". Routes can contain queries (query string) to narrow down returned resources but the route is the location or the resource(s) that's being requested.
With the advent of attribute routing, pluralizing routes for controllers is as easy as adding a [RoutePrefix("quotes")] attribute to the QuoteController declaration. This also allows for easier design for associative routes. Looking at the sample routes provided in the original question:
/quotes
GET: Gets all quotes
POST: Creates a new quote
/authors/shakespeare/quotes
Associative route in the QuoteController
GET: Gets all Shakespeare quotes
/quotes/new
This is a bad route IMO. Avoid verbs.
Make a POST request to '/api/quotes' to create a new quote
/quotes/shakespeare
/quotes/popular
Although these would be nice to have, they're not practical for routing (see below)
The problem with the last two routes is that you have no simple way of differentiating between popular quotes and quotes by authors, not to mention that routes that link to quotes by name or Id. You would need actions decorated with [Route("popular")] followed by [Route("{author}")] (in that order) in order for the routing table to pick the routes up in the appropriate order. But the author route kills the possibility of having routes [Route("{quoteName}")] or [Route("{quoteId}")] (assuming quoteId is a string). Naturally, you'll want to have the ability to route to quotes by name and/or ID.
Slightly Off Topic
Getting quotes by author would best be done by an associative route. You could still probably get away with the popular route as a static route name, but at this point you're increasing route complexity which would be better suited for a query string. Off the top of my head, this is how I might design these routes if I really wanted to have the popular search term in the route:
a:/authors/{authorName}/quotes
b:/authors/{authorName}/quotes/popular
c:/authors/{authorName}/quotes?popular=true
d:/quotes/popular
e:/quotes/popular?author={authorName}
f:/quotes?author={authorName}&popular=true
g:/quotes/{quoteName|quoteId}
These could be spread across the QuoteController's actions. Assuming the controller has a [RoutePrefix("quotes")] attribute:
[HttpGet]
[Route("popular")]
[Route("~/authors/{authorName}/quotes/popular")]
// Handles routes b, d, & e
public ViewResult GetPopularQuotes(string authorName)
return GetQuotes(authorName, true);
[HttpGet]
[Route("")]
[Route("~/authors/{authorName}/quotes")
// Handles routes a, c, & f
public ViewResult GetQuotes(string authorName, bool popular = false)
// TODO: Write logic to get quotes filtered by authorName (if provided)
// If popular flag, only include popular quotes
[HttpGet]
[Route("{quoteId}")]
// Handles route g
public ViewResult GetQuoteById(string quoteId)
// TODO: Write logic to get a single quote by ID
Disclaimer: I wrote these attributes off the top of my head, there may be minor discrepancies that would need to be ironed out, but hopefully the gist how to get these routes to work comes through.
Hopefully this helps to clear up some confusion on the topic of controller and routing best practices on naming conventions. Ultimately the decision to use plural or singular controllers or routes is up to the developer. Regardless, be consistent once you pick a best practice to follow.
Name of controller can be plural or singular based on the logic it executes. Most likely we keep controller name as singular because ProductController sounds little better than ProductsController.
/product/list or /products/list
/product/add or /products/add
You can use both. But you must keep consistency and you should not mix them. Either all URL should be plural for every entity types or all should be singular.
In ASP.NET sample, they have used Singular controller names Such as HomeController, AccountController. In case of HomeController you can't use HomesController because that no longer represents current site Home.
With regards to logic, mostly we create Controller per database entity, in which we infer that Controller represents Actions to be performed on "Entity" So it is good to use Singular controller name and there is no harm.
Only if you want to create set of Controller representing collection of something that should look like or map to plural database table names then you can name that as plural.
Good question. Some contributors to this site would recommend you try Programmers website, but I'm willing to attempt an answer to your question.
Routing mechanism in ASP.NET MVC ,conceptually, is based on Resource-oriented architecture; the common guideline in ROA is
Applications should expose many URIs (possibly an infinite number of them), one for each Resource (any resources in your applications should be unambiguously accessible via a unique URI)
So, it's up to you to decide whether quote and quotes are two different resources or not.

Asp.NET MVC Route Performance

I have an application with a couple thousand routes.
This is because for each product we use a custom URL, as opposed to the textbook /product/id.
With this many urls, the performance is unacceptable in the router.
I am trying to find ways to improve it, but I am drawing a blank.
I have about 20 regex routes and about 3 thousand unique url routes.
Any Ideas?
Sorry for being so open ended, but I am not sure where to start.
If your urls are all on the format yoursite.com/{url}, you can still store all the three thousand urls in the database, and create a custom controller factory which uses the {url} parameter to look the correct information up in the database and assign correct controller, action and any parameters you're using.
There are lots of posts on google on how to implement the controller factory.
I imagine you'll also want some parsing of the existing routes to put them all in the database - this can probably be done by iterating over the RouteTable after you instantiate your application (i.e. after RegisterRoutes() is called).
I would get rid of the 3 thousand unique url routes and replace them with a generic route that is the last catch-all route. Something like:
routes.MapRoute("productRoute", "{category}/{manufacturer}/{productTitle}", new { controller = "Products", action = "Index", category = UrlParameter.Optional, manufacturer = UrlParameter.Optional, productTitle = UrlParameter.Optional });
You could also then add a custom IRouteConstraint to validate that the category exists (but just make sure it doesn't hit your database every time or performance will degrade).

Custom Routing Rules (e.g. www.app.com/project/35/search/89/edit/89)

I would like to create routing rules like the following:
www.app.com/project/35/search/89/edit/48 ---> action is edit in the project controller
The passed variables should be project# (35), search# (89), and edit#(48)
Can someone help me structure a routes.MapRout() for this.
I want to use:
routes.MapRoute(
"Default",
"{controller}/{projectid}/search/{searchid}/{action}/{actionid}",
new { controller = "Home", action = "Edit", projectid = "", actionid = "" }
);
But from previous experience, this type of MapRoute will fail... I've only gotten something in the following format to work:
{controller}/{action}/{variable}
Can anyone give me any advice on this? Thanks.
Honestly, it sounds like you need to make you URL's look like this:
www.app.com/project/35?search=89&edit=48&flag=63
It would make your like much simpler.
Grouping Controllers with ASP.NET MVC
A question that often comes up is how do you group controllers when building a large application with ASP.NET MVC. Often, the question is phrased as whether or not ASP.NET MVC supports “Areas”, a feature of Monorail. According to the Monorail documentation,
MonoRail supports the concept of areas, which are logical groups of controllers. All controllers belong to an area. The default area is an empty (unnamed) one
While there’s no out of the box support for this in ASP.NET MVC, the extensibility model allows building something pretty close.
Registering Routes
The first thing we do is call two new extension methods I wrote to register routes for the areas. This call is made in the RegisterRoutes method in Global.asax.cs.
routes.MapAreas("{controller}/{action}/{id}",
"AreasDemo",
new[]{ "Blogs", "Forums" });
routes.MapRootArea("{controller}/{action}/{id}",
"AreasDemo",
new { controller = "Home", action = "Index", id = "" });The first argument to the MapAreas method is the Routing URL pattern you know and love. We will prepend an area to that URL. The second argument is a root namespace. By convention, we will append “.Areas.AreaName.Controllers” to the provided root namespace and use that as the namespace in which we lookup controller types.
http://haacked.com/archive/2008/11/04/areas-in-aspnetmvc.aspx
Why did this format fail for you? What do you mean by "failed"? If it just didn't work, try placing your new MapRoute above the default commands - the Routes collection is working in a FIFO manner.
#Robert Harvey - Take a look at Basecamp for example - they are using a very similar approach in their URI's. I think it's a much cleaner and better approach than what you're suggesting. It's not even "Simpler" once you get the hang of routing.

Translate a url to a route

I feel like I'm re-inventing the wheel here, but I need to find a way of taking a URL from a catchall,and redirecting that to another route.
The reason for this is because I need to do some things like adding session cookies for certain urls, and then pass them on to their relevant action.
What's the best way of implementing this?
Thanks in advance for any help!
You can try changing the master page at runtime. It's not the most beautiful solution though.
Theming Support
If I understand question...
Looking at your previous question (redirect-to-controller-but-with-a-different-master-using-a-catchall-wildcard) you want to take the wildcard from:
routes.MapRoute(
"Partner",
"partners/{partner}/{*wildcard}",
new { controller = "Partners", action = "PartnerRedirect" }
);
And in the Partners/PartnerRedirect action, send it to the right controller/action specified in the wildcard?
I have no idea the best way to do this, but looking at ways to unit test url routing brought up this:
http://weblogs.asp.net/stephenwalther/archive/2008/07/02/asp-net-mvc-tip-13-unit-test-your-custom-routes.aspx
I'm not sure if it is an exact fit (ie. might not have to mock stuff), but it looks like all the data is returned from GetRouteData to pass to RedirectToAction method.
Oh, so you don't want a redirect? You want the URL to stay:
mysite.com/partners/pepsi/products/cola.htm
but serve up:
mysite.com/products/cola.htm
I don't have MVC at home, but couldn't you instead define your route differently?
Instead of:
routes.MapRoute(
"Partner",
"partners/{partner}/{*wildcard}",
new { controller = "Partners", action = "PartnerRedirect" }
);
do:
routes.MapRoute(
"Partner",
"partners/{partner}/{controller}/{action}/{other stuff you need}",
new { /* whatever defaults you want */ }
);
The action now has the partner variable to do whatever with, and you could add route constraints to partner so only valid ones match.
(I think) you could then use an action filter on your actions/controllers to do appropriate action based on the partner paramter (it should be in httpcontext of the filter, again I think!) so that you don't have to repeat code in every action if you want to do some basic checks/actions on partner.
Here is a decent write up on a lot of ways to skin that cat:
http://weblogs.asp.net/stephenwalther/archive/2008/08/12/asp-net-mvc-tip-31-passing-data-to-master-pages-and-user-controls.aspx
No, it is effectively a redirect, but the redirect is not visible to the user (in much the same way as a Server.Transfer() is done in Web Forms.
The link you pointed at only uses one master page, whereas I have many master pages (close to 200).
I have thought of having separate route handlers for these partners, but the master page choice came into the equation.
I think I'm going to have to create a ViewExtension to overcome these problems...

Resources