We are converting an existing Webforms site to MVC. The current URL structure is like http://site/City-Category-State, where spaces in city and category are also hyphens, so the URL would be /New-York-Small-Business-NY. A company URL structure is http://site/Company-City-Category-State so that would be /Company-ABC-New-York-Small-Business-NY. As you can see there is really no way to map it back to Category or Company Controllers directly. This is why we are thinking of using a declarative routing where an XML document will map URLs to controllers. Where can I put the code to read XML and assign controller/actions to the route?
if you have a siteController with an index method and route everything to that, then you can have your index method take the whole "City-Category-State" as a parameter and parse it, then redirect to the appropriate controller with the right values, or just return the view right there.
Related
i have a PropertyController, which I use to serve a bunch of pages. For example..
/Property
/Property/{id}
/Property/add
/property/edit/{id}
I now need to do a bunch of stuff based on a particular property I will need to do serve pages like this:
/Property/{id}/images/add
/Property/{id}/images/edit/{id}
/Property/{id}/rooms/add
/Property/{id}/rooms/edit/{id}
I think I need to build a new ImagesController and RoomsController, but do I need to but these in a folder structure? My RouteConfig is currently set to the default MapRoute rule ({controller}/{action}/{id}
You don't need to reflect your routing structure in your folders structure.
Check this one out:
ASP.Net MVC support for Nested Resources?.
Effectively your routing string is a regExpression to match whatever comes in from a requester. And if there's a match it's trying to bind all the variables in your expression to values from the HTTP request.
In regard to creating new controllers - a rule of thumb is to create a controller per resource / business entity. So in your case I would say yes to ImagesController, RoomsController and PropertyController.
I am currently working with mvc4 and have a question around best practice.
I am passing back to my view, a number of links based on product information eg. product/1234 etc.
What is best practice, create the link using the routing engine in the controller and return the url as a property on the model object OR return the information to the view and generate the link there? I use automapper to map my DTO objects to model object, also considering creating the links during mapping.
What is the best practice with this?
You always create the link on the view.
The HTML helpers in the view can be used to ensure the link conforms to your routing rules.
You can see this in action in the many official ASP.NET MVC 4 Tutorials.
Why not in the controller or model?
The HTML helpers in the view are designed to create not just the URL, but also to wrap the URL in a fully formed anchor tag, etc. It isn't appropriate to have HTML in your model or controller as they shouldn't care how the data is displayed.
For example, the Html.ActionLink helper returns an a element.
I'm looking to heavily customize the way my application handles Routes. Essentially, I want to achieve a process something like this:
User requests URL: /custom-url-part-1/custom-url-part-2/gallery/1/date
Application looks for a match in a database table of Routes
Application returns matching Route record, consisting of:
RouteUrl
Controller
Action
Parameters
Defaults
Application looks at RouteUrl and parses any optional parameters {pagenum}, {orderby}
Application checks if these have been supplied in the requested Url
If they have not been supplied, it checks the "Defaults" of the Route record to get them
These are then passed in the RouteData defaults
Parameters are passed in the RouteData defaults
Route data is returned
The core reason for this is I want to have a dynamic table of Routes, that is modified by a CMS in order to store certain data against routes.
The questions I have are:
How can I replicate the way that MVC matches a Uri to a Route? I need to essentially look through the database table and bring back the appropriate Route based on the Uri. Very similar to how the standard ASP.NET MVC functionality looks through the RouteCollections and uses the correct route.
I realise I'm essentially rewriting some functionality that MVC supplies built-in, and replacing the RoutesCollection with a database... but I need to do that in order to achieve two things:
The routes being entirely dynamic, fluid and controlled by the CMS.
Data being stored against those routes which can't be inferred from the Uri itself (so for example, I might want to send a PageId through when a user hits a particular route, but I don't want that PageId in the Uri).
I'm looking at developing an application that will include a CMS. I'm a seasoned web forms developer but only really just moving into MVC.
I have a couple of questions that I hope some of you guys can answer:
First, my current web forms CMS allows users to create a page, and then "drop" any number of user controls onto that page they have created. The way I do this is to create an entry in the DB together with the path and then use the LoadControl method.
I can see I can do this with partial views, but partial views have no code behind. If I've potentially got 100 controls that people can drop onto a page, does this mean that the ViewBag in the controller needs to cater for all 100 controls just in case they are used on the view? For example, a web forms user control will contain logic: rptItems.DataSource = blah; rptItems.DataBind()
With MVC, I'm assuming that logic will be in the view controller and the view would access it by the ViewBag? I'm a little confused at how to do this.
Secondly, how would you handle deep routing?
EG:
Store/Products/Category is fine, but what about Store/Products/Category/Delivery/UK ? Would I need to set up a route in global.asax for each route I need? In web forms, I just called the ReWritePath method and handled the routing myself using regular expressions.
Thanks for the time to read this, and hopefully answer some of my queries
For your second question, (ie, "deep routing"), you can handle this within your controller instead of adding real routes. Each part of the url is available via the RouteData.Values collection inside of your controller action. So, your route may look like
~/Store/Products/Category/{*params}
Assuming typical route configuration, this would call the Category(...) action method on ~/areas/store/controllers/storeController, which could then grap delivery and uk from the RouteData.Values collection.
There are a lot of other approaches to this - storing routes in a database and using associated metadata to find the correct controller and method - but I think this is the simplest. Also, it may be obvious, but if you really only need two parameters beyond 'Category' in your example, you could just use
public ActionResult Category(string category, string region)
{
...
}
and a route:
~/store/{controller}/{action}/{category}/{region}/{*params}
Delivery and UK would be mapped to the the category and region parameters, respectively. Anything beyond uk would still be available via the RouteData.Values collection. This assumes that you don't have more specific routes, like
~/store/{controller}/{action}/{category}/{region}/{foo}/{bar}/{long_url}/{etc}
that would be a better match. ({*params} might conflict with the second route; you'll have to investigate to see if it's a problem.)
For your first question:
You can dynamically generate the view source and return it as a string from the controller, eliminating the need to pass a lot of stuff via ViewBag. If a virtual page from your CMS database requires inclusion of partial views, you would add the references to those components when generating the page. (This may or may not address your problem - if not, please provide more information.)
Is it possible to define a route in the RouteCollection of Asp.net MVC, so that it just does the "URL rewriting" part and ignore the URL generation with Html.Actionlink(...)?
In fact I want to add a keyword between controller and action (controller/..keyword.../action) for certain very special requests. The generated URLs on the pages, however, should remain using the default routes. (controller/action)
Yes you can do what you are asking. You would just create your own route that inherited from the current one and override GetVirtualPath to always return null. This way no Action lookup would be done, but the URL would still function as a routing mapping to your action/controller.
Also by the way, what is happening isn't URL Rewriting, because you using the Routes to define endpoints in to your application. Or in other words an API. So no rewriting is taking place. Think of the relationship between routes and your action/controller as more of a publically defined namespace for the web. Just like a namespace you are defining a specific spot in your application where your action/controller can be found.