Another ASP to MVC migration > routes - asp.net-mvc

Migrating our classic ASP site to ASP.Net 4.5 MVC. Our site is pretty large, hundreds of file, and SEO optimized. All URLs are SEO friendly, via web.config, eg.:
www.ourdomain.com/articles/2013/120/the-best-article
www.ourdomain.com/blog/2013/122/the-best-blog
www.ourdomain.com/video/2013/123/the-best-video
ie. www.ourdomain.com/{contenttype}/{year}/{id}/{url encoded title}
The ASP files to render are stored in a folder like /render/content.asp
We don't want to change any URLs in this migration. The new files of the MVC app will be in all new directories than the existing site. For example, a view: /MVC/content.vbhtml
How would you go about adding links in your Views, with ActionLink or other, to use the existing SEO friendly path instead of the default path, without hardcoding the path in the links in the view (adopt from currently URL, more or less). For example:
#Html.ActionLink("Edit", "Edit", New With {.id = currentItem.ID})
That creates an Edit link like : /MVC/Edit/120
But, what I would like is: /article/edit/120.
Any help appreciated.

Use
#Html.RouteLink("Edit", "myRouteName", New With { ... route params ... } );

Related

Deployed mvc app not including sitename in href links

I have an asp.net MVC web app using Kendo UI Grids with client templates. The client template specifies an href as follows:
.ClientTemplate("<a href='[controller]/[action]/[parameters]' />")
In a debug run, the link is correctly generated as follows:
http://localhost:[port]/[Controller]/[Action]?[parameters]
works beautifully.
However, in a deployed environment, the link generated is as follows:
http://[server]/[Controller]/[Action]?[parameters]
And that fails because it is missing the website name. So what I need is for it to generate the links as follows:
http://[server]/[WebSite]/[Controller]/[Action]?[parameters]
How do I need to specify my href links for this to work?
Don't hand-code your url's instead of use the UrlHelper which will take care of including the virtual directory into your urls:
.ClientTemplates(string.Format("<a href='{0}' />",
Url.Action("action", "contoller", new { param1, param2})))
If you want to use the client template value in the url you have to build the url dynamically because the Kendo UI template syntax won't inside the route value:
columns.Bound(m => m.Id)
.ClientTemplate(string.Format("<a href='{0}&Id=#= Id #'>Click me<a>",
Url.Action("action", "contoller", new { someNotTemplatedParam })));

Url Rewriting in Umbraco 5 Jupiter

i want to do simple URL re-writing in Umbraco 5 like we used to do in previous versions i-e changing the UrlRewriting.config file, or we used to do it in Asp .Net
<rewriter>
<rewrite url="~/en/faq.aspx" to="~/faq.aspx" />
</rewrite>
I know we can create new area in the application and then add new routes in areaRegisteration.cs. but then the problem of mapping these controllers to the default Views arises, as the views are in: Views > Umbraco Directory of the application.
For example,
I create a new Area Named "En" and add Route in EngAreaRegisteration.cs, i-e
context.MapRoute("en_property","en/{city}/{controller}/{action}",new { } );
then how i can map the Actions to any Umbraco Page, i dont think the Redirect is a solution as redirect will change the Url,
my URL will be like this, en/paris/property/book
i am using Umbraco 5.0.1 Web deploy.
Thanks in Advance
Sher

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.

Considerations when turning on RouteExistingFiles

I am looking to generate some CSS files dynamically in my Content folder.
At the moment, this folder has an ignore route (routes.IgnoreRoute("Content/{*wildcard}");) and, I'd like that to remain, as I don't need/want most of my content folders to go into the MVC request lifecycle.
Example:
routes.MapRoute(
"DynamicCSS",
"Content/Css/Dynamic/{guid}.css",
new { controller = "Site", action = "GenerateCSS" },
new { guid = #"^([0-9a-fA-F]){8}([0-9a-fA-F]){4}([0-9a-fA-F]){4}([0-9a-fA-F]){4}([0-9a-fA-F]){12}$" }
);
//If the file has already been generated, IIS should just return the file, saving a request in MVC.
routes.RouteExistingFiles = true; //was formerly false
//Ignore routes
routes.IgnoreRoute("Content/{*wildcard}");
I have a couple questions/concerns about this setup:
Will this work? Routes in ASP.NET MVC are lazy, but I don't know if the ignore routes are checked first. There's no documentation (I've Googled!) on this form of usage.
Are there any security implications to consider when switching on RouteExistingFiles? I don't want IIS to pick up any of my Model/Views folders by directly referencing them.
Many thanks for any suggestions.
Edit:
After further research, I have found an article on my first issue.
Scott Hanselman got a blog post "Plug-In Hybrids: ASP.NET WebForms and ASP.MVC and ASP.NET Dynamic Data Side By Side" in which he talked about his too. http://www.hanselman.com/blog/PlugInHybridsASPNETWebFormsAndASPMVCAndASPNETDynamicDataSideBySide.aspx
Hope this helps,
Ray.

ASP.NET MVC - Is it possible to generate cross-application action links?

is it possible to generate cross-application action links using the HTML helper class?
I have the following (both are separate VS2008 projects):
http://mainwebsite/
http://mainwebsite/application
I would like to generate a link IN the /mainwebsite/application/ project TO /mainwebsite/. Is this possible? Or should I just hardcode a hyperlink?
addition:
My question also applies vice-versa. So generating a link IN /mainwebsite/ TO /mainwebsite/application/. I already managed to do like so, by simulating the application name as controller name:
<% =Html.ActionLink("ApplicationName","",new With {.Controller = "application" }) %>
Yes, you can do this. The HTML helper generates URLs using the routing system, which is completely ignorant of ASP.NET MVC and your application. If you have a route in your route table for the other application, then you can generate a URL for it using the HTML helper. There is no rule which states that your route table can only contain routes for the current application, although you obviously need to be careful about how you order the routes.

Resources