ASP.NET MVC One Way Route - asp.net-mvc

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.

Related

Do I need a new controller for this?

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.

Using custom routes instead of /controller/action/{id}

I have to make vanity urls for an already existing site. The requirements are:
The existing controllers are a kind of hierarchical structure and can't go away or be overridden in any way. Examples: domain.com/Dog/vanity and domain.com/Cat/vanity.
Keep existing actions. Any existing actions must take priority so that the page for that action is not stomped on by the vanity url.
take future pages and actions into account so that the above requirement is met (a new vanity url is ignored and the action/view executed instead)
To date, I have tried various solutions with routing that allow me to have domain.com/vanity which is nice but the marketing guys don't like because of the different departments within the company. I've tried routing which will override the existing actions and treats them all as vanities (also not feasible). I've a solution in place that programmatically deals with the url that was requested and redirects to a page that actually exists but this is not scalable in any way.
So far, I know that the vanity portion can be treated as a parameter to the action so that I can fire off the default page in the route (Index) but this is, so far, doesn't preserve the structure.
TL;DR: I need to have a solution that allows for domain/controller/vanity structure while also allowing domain/controller/action
Using AttributeRouting for MVC4 you can accomplish a working solution until you ramp up the replacement project. It'll allow you to keep existing routes while adding new, custom ones with little impact.
[Route("my-vanity/is-cool/post/{id}")]
public ActionResult Index(int id)
{
}
The important part is to remember priority, so you write routes that don't overwrite/are overwritten by existing routes. You can steer this to some degree with properties on the attribute. Below is just an example that will put the added route last in priority for the entire site.
[Route("my-vanity/is-cool", SitePrecedence = -1)]
public ActionResult Index()
{
}
ASP.NET WebApi2 have built in support for attribute routing. With it you can define URL's in whatever way you like (instead of following the /controller/action pattern)
http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

ASP.NET MVC - Creating a custom Uri to Route mapping handler

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).

ASP.Net MVC redirecttoaction not passing action name in url

I have a simple create action to receive post form data, save to db and redirect to list view.
The problem is, after redirecttoaction result excutes, the url on my browser lost the action section. Which it should be "http://{hotsname}/Product/List" but comes out as "http://{hotsname}/Product/".
Below is my code:
[HttpPost]
public ActionResult Create(VEmployee model, FormCollection fc)
{
var facility = FacilityFactory.GetEmployeeFacility();
var avatar = Request.Files["Avatar"].InputStream;
var newModel = facility.Save(model, avatar);
return RedirectToAction("List");
}
The page can correctly render list view content, but since some links in this view page use relative url, the functions are interrupted. I am now using return Redirect("/Employee/List") to force the url. But I just wonder why the action name is missing. I use MVC3 and .Net framwork 4.
I am new to ASP.Net MVC, thanks for help.
Your route table definitely says that "List" action is default, so when you redirect to it as RedirectToAction("List") - routing ommits the action because it is default.
Now if you remove the default value from your routes - RedirectToAction will produce a correct (for your case) Url, but you'll have to double check elsewhere that you are not relying on List being a default action.
Well, Chris,
If you get the right content on http://{hotsname}/Product/ then it seems that routing make that URL point to List either indirectly (using pattern like {controller}/{action}) and something wrong happens when resolving URL from route or {action} parameter is just set wth default value List. Both URLs can point to the same action but the routing engine somehow takes the route without explicit action name.
You should check:
Order in which you define your routes
How many routes can possibly lead to EmployeeController.List()
Which one of those routes has the most priority
Default values for your routes
Just make the route with explicit values: employee/list to point to your List action and make sure that is the route to select when generating links (it should be most specific route if possible).
It would be nice if you provide your routes mappings here.
but since some links in this view
page use relative url, the functions
are interrupted.
Why do you make it that way? Why not generate all the links through routing engine?
When using the overload RedirectToAction("Action") you need to be specifying an action that is in the same controller. Since you are calling an action in a different controller, you need to specify the action with the alternate overload e.g. RedirectToAction("List", "Employee").

ASP.NET MVC basic routing with no View name

This is a basic question about the routing machinery. From a new MVC project, I have this in the HomeController:
public ActionResult MyPage()
{
return View();
}
In the Views\Home folder, I have MyPage.aspx. The routing is still the default of {controller}/{action}/{id}. When I type in http://localhost:1790/Home/MyPage, it is correclty routes to MyPage.aspx. Since I haven't said anything about which view to render, how does ASP.NET MVC know to correctly route to MyPage.aspx? Looks as though the ActionResult name can also be used as the View/aspx page name...unless there is something I misunderstand in how the routing works. I can see how I end up in the Home folder, since the controller name corresponds to the View sub folder name. But does the Action name also correspond to the aspx name?
Would that work if the page was a PHP?
ASP.NET MVC subscribes to what is known as the Convention over Configuration paradigm whereas if you follow their conventions, basic things such as routing concerns will happen for you. But they also allow you to configure them if desired.
MVC implicitly assumes that if you return just View(), that you want View("MyPage") (i.e. the action name). No sense in repeating yourself unnecessarily.
It won't find a PHP file by default, but I'm sure you could override that behavior if you really wanted to. I can't imagine a sane scenario where you would be mixing PHP and ASP.NET MVC, but who knows :)
Action name is the same as the view / partial view name.
asp.net mvc doesn't work with php as far as I'm aware.
As has already been stated, ASP.NET MVC uses convention over configuration. Out of the box, your folder structure is something like this (only showing relevant portions and doing it from memory so...)
Site Root
+ Controllers
HomeController.cs
AccountController.cs
+ Views
+ Home
Index.aspx
+ Account
Index.aspx
+ Shared
The default routing handler is something similar to the following:
"{controller}/{action}/{id}"
There are default values for the route, but if you have a url that is a/b/c, it will look for action a on controller aController and pass it c as a parameter if said method on the controller accepts parameters.
A couple of things about that then need to be clarified. Again, convention over configuration:
1) All controller classes must end with Controller if you're using the default engine. That way, when a request comes in and the {controller} value is parsed, the engine adds Controller to it, looks in the Controller folder (and, thus, namespace), and locates the class.
2) By default -- this can be changed -- all views for a controller must reside in the Views/{controller} folder or in the Views/Shared folder.
3) Public methods on a controller are, by default, actions. You can hide this with an attribute to make themethod unavailable to the engine, but by default they are public.
So, when a request comes in the route is compared against all known routes (global.asax) and the first route that matches the request is accepted. The route is then parsed into the component parts to determine the controller, action, and parameters for the action.
Once the controller is identified, the engine instantiates an instance of that controller and executes the matching method (action) should it be found.
The action will return an ActionResult. View is an extensino method that actually returns ViewResult (if I remember that correctly). The default view for an action is a view of the same name as teh action residing in the Views/{ControllerName} folder.
Routing is a beast unto itself and I'd recommend a good bit of reading on it if you're going to play with it. Minutes to understand but a lifetime to master sorta thing.
To my knowledge, BTW, there is no engine that will use a php page as a view for a controller action.

Resources