ASP.NET MVC generates URLS starting with '/', are relative possible? - asp.net-mvc

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.

Related

How to always get the correct sub-domain in an mvc application?

I have some MVC applications. They are set up in virtual directories on my local machine. On live they are set up on root website installations.
this means that an image in both scenarios would be...
local
/mysite/images/image1.jpg
dev
/images/image1.jpg
I have looked at the #Url helper but cant see what exactly I should do with it....
You should always use url helpers when generating urls in ASP.NET MVC. For example instead of:
<img src="/images/image1.jpg" />
you should write:
<img src="#Url.Content("~/images/image1.jpg")" />

My _javascript file cannot be found

I have some javascript files that are named starting with an underscore. When I publish these it seems they can't be found. Is there some rule that stops these being viewed by the browser when running on IIS? I think I remember something like that for the cshtml files but didn't know that applies to js files.
I found the following post, which talks about files with "_" prefix: Why does Razor _layout.cshtml have a leading underscore in file name?.
By convention, the Razor pages that cannot be shown by the browser via direct request are prefixed with "_". Following is one of the comments from the post:
Razor was developed for ASP.NET Web Pages (WebMatrix), which doesn't
have the same sort of protection built in regarding Views folders and
Routing that you get within MVC. Since layout pages in Web Pages are
not intended to be served directly, they are prefixed with the
underscore. And the Web Pages framework has been configured not to
allow files with leading underscores in their names from being
requested directly.
In your View is the reference to them pointed to the correct path?
<script src="#Url.Content("~/Scripts/jquery-custom.js")" type="text/javascript"></script>
I would also hit Control + F5 several times after the page loads to make sure it is not cached and that is the reason it is not pulling down.

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

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?

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.

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