Special area route (ASP.NET MVC) - asp.net-mvc

I have a unusual problem with ASP.NET MVC3 Routing. I created a area named "Account" and inside that a controller "Main" with action "Login". Now I wanted to create a route, that would look something like this: "/Login" (that means no "/Account/Main/Login"), but I keep failing to do that (I have used the AccountAreaRegistration to register routes, but #Html.ActionLink always skips them and chooses the default area route ("/Account/{controller}/{action}", thus the URL is different from what I want). How can I proceed and solve this issue?

I have solved my issue, it was actually quite stupid. The order of the routes was OK, but the problem was caused by the fact I did use area = "Account" default value in the list of MapRoute's default values. After I deleted it, everything works like a charm.

Related

How are the default MVC Controllers added to the route table

We says that the MVC request is processed by the controller/action which are mapped in the Routetable.
In routeconfig file, there is only one (default) route is mapped. But how all the controller/action excecuted correctly. How the other routes are added in the routetable ?
The default route works by convention, which looks for classes in the Controllers folder that end with the word controller (i.e. OrdersController). In this case the route will automatically be added to your routing table.
You can find the full documentation for ASP.Net Routing here: https://msdn.microsoft.com/en-us/library/cc668201.aspx
You need to do some reading here to fully understand how ASP.NET routing works.
https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/controllers-and-routing/asp-net-mvc-routing-overview-cs

CakePHP 2.x: How can I overwrite an URL pointing to the index page?

I'm getting this issue:
Router::connect('/',array('controller' => 'Controller','action' => 'login'));
This will show www.mysite/controller/login as the site URL
I would like to overwrite www.mysite.com/controller/login with just www.mysite.com, but still go to the login page. Does anyone know how to do it with Cake 2.x?
The behavior It's not exactly as you describe.
What the following does:
Router::connect('/',array('controller' => 'Controller','action' => 'login'));
is allow you to type www.mysite.com in your browser, and get the view that www.mysite.com/controller/login renders.
It works like an url rewrite instead of a redirect. Therefore, the above should work as expected. However, if it's not an example, try to name your controller differently, as it may cause trouble with CakePHP.
As stated by Inigo Router::connect() just connects a route/URL to a controller action. So with your defined route you should be able to goto www.mysite.com and your login action will be served (although I'm not sure that it is a good idea to have the base URL act as the login page).
It does not prevent www.mysite.com/controller/login from working as this is one of CakePHP's default routes.
To disable the default routes you need to remove this line from routes.php:-
require CAKE . 'Config' . DS . 'routes.php';
Be warned, if you remove this line you must have defined routes for all your pages in your app's routes file. This is not necessarily a bad thing, Beware the Route to Evil is a good read in regards to this.
As I used the "Auth" component I had to add in the function
beforeFilter()
of my controller this line:
$this->Auth->allow('anAction', 'anotherAction', '**login**');

Generating Outbound Links To Main Website From Widget

I've got an ASP.NET MVC 3 web application that lives on server 1:
http://www.mysite.com
And i've got a widget (also ASP.NET MVC 3 web application) that lives on server 2:
http://widget.www.mysite.com
Now, i want to generate a link to the main website from my widget.
So if i do something like this in the widget project:
#Html.RouteLink("Outbound link", "MainWebsiteRoute", "http", "http://www.mysite.com", string.Empty, null)
It will error (or generate the wrong link) because the route tables are different between the widget and website project.
Even if i copy the route from the main website into the widget one, the controllers/actions don't exist in the widget project, so it won't work.
How do i do it? Do i have to hardcode the URL's?
Ended up duping the routes from the main website that i need in the widget.
Example route in main website:
context.MapRoute(
"Q&A - Individual Q",
"{locationUniqueUri}/questions/{questionUniqueUri}",
new { controller = "Questions", action = "Detail" },
);
Duped route in widget:
RouteTable.Routes.MapRoute(
"Outbound - Q&A - Individual Q",
"{locationUniqueUri}/questions/{questionUniqueUri}"
);
Notice how there are no route defaults - that's because i don't care, since that's concerning matching the route to a controller/action. But this happens in the website, not the widget. All i'm concerned about is generating the URL.
I then created a simple wrapper for RouteLink called OutboundRouteLink which specifies the hostname (for the main website), and generates the link.
I don't think it's too bad a solution. Obviously, it's hard-coded in a way - if the routes change in the main website then i'll have to change the widget routes. But that fine, because if one of my URL's changed, i'd have to do 301's anyway, so the old URL's would still work for the widget (they would get 301'd).
Also, i could go one step further and store the route url in a config file and share that between the projects, which would give me a bit more safety. But there's only around 3 outbound URL's so this should be fine.
Not saying this is the best way to handle it, but im happy with it.
Are you using areas? If not, then perhaps you should be. That way you could simply supply the area for the route and it would be able to resolve the url for you. Given your sample URLs you could build your main site with your widget site as an area. Supplying a empty string for the area parameter in the route values would allow you to reference a controller/action in the main site from the widget site.

Using MVC Routes as Shortcodes

We have been trying to implement shortcodes on an ASP.NET MVC web app that allow users to uniquely invoke a given article/page using an assigned short code.
For e.g.: www.mysite.com/power would map to an actual URL: www.mysite.com/Power/Home/.
I have created various routes throughout the site that map these shortcodes to various actions and controllers within the application. From a shortcode/route point of view, everything is working great.
I, however, noticed a couple of interesting things. I have hyperlinks that I use Url.Action method to generate the URL pointing pages. Many of these pages also have short codes associated with them. For e.g.: I have a link that says:
Go to Power page
This is a page that also has the previously mentioned short-code assigned to it. When I use Url.Action, I ideally expect it to create a link as /Power/Home/Index or /Power/Home, but since I also have a route constraint mapped to it, it now generates the link as /power.
Is there a way I can just use the actual link URL when generating links? I only want short-codes when I am sending out emails etc. I want the site to generate actual URLs.
This may or may not be possible, but I wanted to see if there were any ideas out there that I could use.
Anup
Index and Home are likely defined in your route table as defaults for the Action and Controller element. When you generate the Url it wont include the defaults if they aren't needed.
You could write your own Action overload or helper, which would allow you to take more direct control of the generated URL or action link. You could approach it from two different ways: 1) a helper to generate short-code specific urls and links, and/or 2) a helper to generate the full url and/or link. If Url.Action is returning the short-code version due to your routing configuration, I'd think a good place to start would be the second option, creating a helper/extension method that will generate the full url for you.
Here's how I solved this:
Instead of naming a route with short code to point to the action url, I made the route point to a different Controller action which would then redirect to the actual route that I want it to.
For e.g.: Originally I had the code "power" defined in the route table such that it would point to www.mysite.com/Power/Home.
Now instead of pointing it to that action - Index, controller - Home, area - Power, I make it resolve to: action - Power, Controller - Home, Area - ShortCode.
In the controller now, I simply do a RedirectToAction("Index", "Home", new { Area = "Power" });
This ensures that the actual links to /Power/Home do not resolve to the shortcode "power".
This is a simple fix increased the work by a little bit, but works like a charm.

aspx url to mvc controller action

I have an aspx page Test.aspx. It handles requests like
Test.aspx?First=value1&Second=value2&third=value3
How can I use routing to redirect this url to
TestController/MyAction?First=value1&Second=value2&third=value3
I know I can create an aspx and perform redirect in it`s page load. But seems ugly and I think it can be done with some custom route.
What I`ve tried was: this solution
but it didnt work for me.
I remember, that Test.aspx should not be on a disk. I don`t have it, and routing is still not working. Have no idea on what can cause this issue.
Have you tried adding a route like the following:
routes.MapRoute(
"Test",
"Test.aspx",
new { controller = "TestController", action = "Show" }
);
Just remember that the route will not work if the Test.aspx file is still on disk.
Also, ideally you would want to have a permanent redirect so the search engine links etc get updated to point to your new urls.

Resources