asp.net mvc url of anchor's start with "/" is wrong? - asp.net-mvc

in my Asp.Net MVC web project i always use anchors url as
which is making my urls working good. output is like below
if in host
if in development
I am using Search Engine Optimization Toolkit (http://www.iis.net/download/seotoolkit) for finding errors in my webpage.
What I see in results is "broken hyperlinks". When I looked to the results link that start with "/xxx" is not like "domain.com/xxx" seo toolkit sense url like "domain.com/currentpage/xxx".
What is the problem is in here?
Am I doing wrong with writing links start with "/" ? Is it wrong?

Instead of hardcoding urls I would recommend you using HTML helpers:
<%= Html.ActionLink("link text", "someAction", "someController") %>
This will generate proper relative URLs no matter whether you are running on development or production server inside virtual directories, etc...

I suggest that you mark your anchors as runat="server" and change / with ~/ in your URLs.
Let ASP.NET resolve your relative paths to absolute ones.
"/" is for specifying that your path starts at the root of your Web Site. That's good in most of the cases, but what happens if your application is nested in some other virtual directory? Your URL would be invalid in this case.
"~/" resolves your path as an absolute path, but of your Web Application, that's you've nested applications, this "magic" will render the full path to the application's folder.
Does it help?

Related

Running MVC application on IIS without domain

My application contains lots of links to the root ("/login/dologin"). When I'm running the application under a domain, there's no problem.
Right now I'm moving to a new server, and I can't test my application. My application sits in "localhost/md", I need the link to go to "localhost/md/login/dologin". Instead, it goes to "localhost/login/dologin", and, ofcourse, the resource cannot be found.
What do I need to configure on my IIS to make this works without domain?
Thanks.
It's just a guess, since you haven't posted any of your configuration.
In your authentication element in the web.config, do you have the route to the login page specified as /login/dologin? could you try ~/login/dologin
The second option, should give you a relative path from the home of the virtual directory application, rather than going to the root of the 'site'
For referencing files (e.g. javascript & css) you could do #Url.Content("~/path/to/file.js")
EDIT: Based on additional comments
in Layout.cshtml...
var SITE_ROOT = '#Url.Content("~/")'
then in your JS file use SITE_ROOT as a prefix in your routes
var url = SITE_ROOT + "Home/Index";

ASP.NET MVC server path is different from application path

I have an unusual circumstance where our web server inserts a folder into the url path before loading the page. Let me give you an example:
The app is called equipment and if I were to run it on a normal server setup, it would look like:
www.site.com\equipment\home\index
BUT when I run it on our server, it inserts "idn" in the url:
www.site.com\idn\equipment\home\index
The messes up my relative references. The MVC functions want to redirect to use "\equipment\" instead of "\idn\equipment\". This happens with Scripts.Render(), Return View(), etc.
Also, I can't hardcode the "idn" into my urls b/c then it is no longer relative and it won't work on my dev box or test servers b/c they don't have a "idn" subfolder in localhost.
I also tried functions such as Request.ApplicationPath and what not but none of them return the "idn" in the result.
Is there way to MVC to know that this "idn" was inserted into the url and account for it?
Thanks!
Create your application on the test/production server in the idn folder, then it all works.

asp.net mvc using HREF in application running on IIS

There is a partial view representing pager control (very similar to this) for blog content. Code generates HTML with references and href like, "/Blog/Posts/Page/1", "/Blog/Posts/Page/2" etc.
It worked absolutely fine on Cassini, but after I switched to IIS problems appeared.
IIS application running in virtual folder, so URL is
http://localhost/tracky
and blog area located,
http://localhost/tracky/blog
As I press on pager button, I recieve 404, because the URL would be
http://localhost/blog/page/3
Instead of
http://localhost/tracky/blog/page/3
My question is, how to handle such situation? how to change code generation to provide correct URL? how to make it work same - as root applicaton or application in virtual folder?
Source code is here
You need to generate your urls either by using ActionLink in your view, or using an UrlHelper in your href as follows: <a href="<%=Url.Content("~/blog/page/3")%>" ..>bla</a>. This will generate Urls that are adjusted accoring to your application root.
You should be using the following:
UrlHelper.GenerateContentUrl("~/Blog/Posts/Page/1");
The ~ allows the url to be resolved relative to the application path and should produce correct results in both cassini and IIS.

ASP.NET MVC generates URLS starting with '/', are relative possible?

ASP.NET MVC helpers generates URLs with slash, I use <base /> tag for my project, so it possible to place application to folder. Is it possible to generate relative URLs without first slash?
Thanks!
Use this syntax for your images...
<img src="<%=ResolveUrl("~/Content/images/mylogo.jpg")%>"/>
...and all of your URLs will be calculated based upon the root of the domain, making them work in any page.

what is the best practices around links and url in asp.net-mvc

looking at different sites, i see conflicting conventions.
if you want to have links to images, other pages, js files, i have seen:
URL.Content("~/scripts/myscript.js");
<a href="/scripts/msscripts.js">
<img src="../../images/test.jpg" />
<img src="../images/test.jpg" />
<img src="/images/test.jpg" />
<img src="~/images/test.jpg" />
these all seem to work in asp.net mvc but it seems like there are all doing slightly different things.
I am moving to a new webserver where they are changing from IIS redirecting to isapi rewriting and i was told to make sure my links were done in a correct way or the site not work.
Can someone clarify what the "correct" way is ?
Use the first option. That way, you can relocate your application to any subdirectory or top level of your domain.
The second option is fine, if you know for sure that the application will NEVER run from a subdirectory (i.e. it will always be at the server's root).
The third and fourth are bad, because they refer to your URL structure, which is meaningless in a MVC environment: add a new bit to the path (one more argument for example) or refactor the paths, and nothing will work.
The best way is to use Url.Content, and make sure you have the ~ in it.
Url.Content will then replace the ~ by the correct path (the root of your website).
Only use relative paths in your css file.
If your css is located in
/css/style.css
and a background image in /images/background.png
then in your style.css use:
#divTest
{
background-image = url("../images/background.png")
}
Since your website gets moved around as a whole this will keep working.

Resources