MVC: Simplest way to create a link to the application base URL? - asp.net-mvc

I have a page where I want to include a "Home" link which takes me to my application's base URL. So far the simplest way I managed to achieve this is through the following line of Razor code, but it's not pretty and I'm not terribly confident about it:
#Html.RouteLink(MyResources.HomeLinkLabel, new { controller = "" })
Note that if I don't include controller = "" then the hyperlink it generates takes me to the current page, not my base URL.
I feel I'm missing something obvious... What's the correct way of doing this?

You could use the following code to get the root URL
Url.Content("~/");
The server-side ~/ syntax references the root of your application (meaning it will take into account if your app is registered in a virtual path in IIS).

If you want to go to the an specific action, you could just include the controller name and the action you want to go:
#Html.RouteLink(MyResources.HomeLinkLabel, new { controller = "Home", action = "Index" })
Now, if you want to go to the root, you can just put something like
...

You could also just write regular ol' HTML:
#MyResources.HomeLinkLabel

Related

Is there a simple way to map a single URL to another URL in ASP.NET MVC?

I have a working ASP.NET MVC application to which I would like to add a very simple URL which I would like to be redirected to another URL, without the Controller part of the path in it.
I.e. I want to be able to tell people to go to mySite.org/Hello and have it direct them to mySite.org/Whatever/WhateverElse?myfun=9, or whatever.
So I added a method in PublicController that redirects Hello to whatever, which works, except it requires me to tell people to go to mySite.org/PUBLIC/Hello, and we'd like to not have the extra word "public" in there.
Hours of confusing study later, I see that I could perhaps add a line in Global.asax.cs such as: routes.MapRoute(name: "Hello", url: "Public/Whatever"); ... or something... but that actually changes the way everything gets routed, and after trying various syntactical variations, nothing has seemed to just change one basic address to map to another basic address.
Is there a simple way to get mySite.org/Hello to map to another URL, without the virtual subdirectory for the public controller?
Create a custom route (it would need to be the first route in the table)
routes.MapRoute(
name: "Hello",
url: "Hello",
defaults: new { controller = "Whatever", action = "WhateverElse" }
);
then /Hello will redirect to /Whatever/WhateverElse

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.

How do I use an ActionLink with the RouteValueDictionary?

I'm trying to make my URL look like this:
http://domain.com/controller/action/123/SomeTextForSEO
I tried using an Action Link, but that appends ?company=SomeTextForSEO instead of the company name after a slash.
<%: Html.ActionLink("DomainList", "Index", "DomainList", new { id = item.CompanyID, company = item.CompanyDisplayName.Trim() }, new object { })%>
and now I think I need to use a RouteValueDictionary but I'm unsure of how to do this in ASP.NET syntax. Can someone point me in the right direction? The MSDN examples are insufficient.
If you go to global.asax.cs and add a new route similar to the existing default route with the pattern
"{controller}/{action}/{id}/{company}"
Before the default one, you should find that the link will generate correctly with your ActionLink call as-is.
I'm on a phone so I'd like to be more verbose (a route restriction would be a good idea to prevent this route interfering with the default), but the HTC keyboard is not code friendly ;)

I'm getting a "Does not implement IController" error on images and robots.txt in MVC2

I'm getting a strange error on my webserver for seemingly every file but the .aspx files.
Here is an example. Just replace '/robots.txt' with any .jpg name or .gif or whatever and you'll get the idea:
The controller for path '/robots.txt'
was not found or does not implement
IController.
I'm sure it's something to do with how I've setup routing but I'm not sure what exactly I need to do about it.
Also, this is a mixed MVC and WebForms site, if that makes a difference.
You can ignore robots.txt and all the aspx pages in your routing.
routes.IgnoreRoute("{*allaspx}", new {allaspx=#".*\.aspx(/.*)?"});
routes.IgnoreRoute("{*robotstxt}", new {robotstxt=#"(.*/)?robots.txt(/.*)?"});
You might want to ignore the favicon too.
routes.IgnoreRoute("{*favicon}", new {favicon=#"(.*/)?favicon.ico(/.*)?"});
You can adjust the regular expression to exclude paths.
Haacked from the source.
The ignore route given above didn't work for me but I found a similar one that did:
routes.IgnoreRoute("{*staticfile}", new { staticfile = #".*\.(css|js|gif|jpg)(/.*)?" });
This error could also happen if inside a view in your area, you use the Html.Action helper. This helper will always use the area as a prepend, unless you specifically tell it not to. E.g.,
#Html.Action("Main", "Navigation", new { area = string.Empty })
I found another solution too... While I don't think I'll use it, it's worth showing here in the answers:
The following should (in theory) ignore looking for controllers for anything with a '.' in it.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" }, // Parameter defaults
new { controller = #"[^\.]*" } // Parameter contraints.
);
Do you still have:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
... in your Global.asax.cs?
MVC puts it there by default, and it's supposed to handle this.
If you do, then the problem may be how you're mixing MVC and WebForms.
I encountered this error when I request resources that did not exist.
Specifically, I was requesting a custom IE css file:
<!--[if lt IE 8]>#Styles.Render("~/Content/ie7.css")<![endif]-->
(These are condition comments, interpreted by IE)
However, the actual resource existed on ~/Content/ie/ie7.css.
So, without any modifications to the routing, the error was solved by using the correct url of the resource.

how to display Flash (swf) content in ASP.NET MVC

In our WebForms apps we serve flash via simple anchor tags like so:
See It
Now, I'm wanting to move that A tag into a call to a Controller/Action using an Html.ActionLink like so:
Html.ActionLink("See It", "DeliverFlash", new {fileName="whatever.swf"})
Then in the controller I'm using a FileStreamResult to push it out....
That "works" in that the flash goes out BUT....
1) It only prompts the user to download the swf I'd like it to just show it like the original implementation does.
2) I have not yet figured out how to pass along those extra parameters of class and params.
Can anyone help please?
Make sure that when you create the FileResult, that you don't set the FileDownloadName property or it will add a Content-Disposition header to specify it as an attachment. See the source code at: http://www.codeplex.com/aspnet. To set the extra parameters, you simply need to use a signature that includes the html options.
<%= Html.ActionLink( "See It", "DeliverFlash",
new { fileName = "whatever.swf" },
new {
#class = "something",
#params = "width, height, yadda, bang"
} ) %>
Note the # in front of class and params as they are C# keywords.

Resources