What determines the current directory in a web application - asp.net-mvc

I have a MVC web application.
The start url is //foo/login/index.
On a view I show a picture images/bar.jpg.
The image is in de directory //foo/images
On my working station this works but when I install it on the test server the site can't find the image because it url to the image is:
//foo/login/images/bar.jpg instead of //foo/images/bar.jpg
So my current directory on my workstation differs from that on the test server.
My question: What determines the current directory in a mvc web application

You probably have something like this in your View:
<img src="images/bar.jpg">
Replace it with:
<img src="~/images/bar.jpg">
The tilde or ~ character is resolved by the rendering engine at runtime to the root (home path) of your website.

Related

URLs pointing to wrong virtual directory on IIS

I have an IIS web application called "WebApp" created inside the "Default Web Site". The WebApp has a virutal directory with a repository of images called "Repository".
Schema:
-Default Web Site
-WebApp
-Repository
When I execute the web application like this:
http://localhost/WebApp
All my images points to a relative url like "/Repository/ImageDir/image.jpg" which is interpreted like http://localhost/Repository/ImageDir/image.jpg.
That URL is wrong, because since my virtual directory is inside WebApp, the correct URL should be http://localhost**/WebApp**/Repository/ImageDir/image.jpg
How can I make it generate a correct URL.
Just for more info, the web application is in ASP.NET MVC.
UPDATE:
I'm using Razor in my views, so here is the code to generate a img tag:
<img src="#m.ThumbPath" />
Where "m" is an element of the Model (which is a List of elements) and "ThumbPath" contains the relative path that is giving problems.
Since you are using Asp.Net MVC you should be using Url.Content-helper to generate the url to resources inside your app:
<img src="#Url.Content(String.Format("~{0}", m.ThumbPath))">

Problems deploying MVC 4 application to website already hosting classic ASP

I am in the introductory steps of teaching myself ASP.Net MVC 4 with the intent of converting old Classic ASP code. So, I have made a simple Hello World application in MVC4 and want to deploy it to our development server. The development server currently mirrors our production environment. The production environment hosts a bevy of Classic ASP pages, hosted in the "web" directory.
I have used the publish button in VS to upload the hello world page to the dev server, dropping it into the "web" directory. So, within the "web" directory, there are 2 new folders /bin and /Views. The actual page I created resides within the /Views/Home directory.
After the code was uploaded I pulled up IE and attempted to view the hello world page by navigating to localhost/views/home/index.cshtml and I received a 404 error.
I then pulled up the directory listing in IIS and navigated down to the "home" directory. I right-clicked this folder and selected "convert to application".
At this point I was able to see my hello world page by directing IE to the root localhost. However, after removing the "home" directory from the application list (essentially undoing the "convert to application"), the hello world page still shows when I pull up the root directory in IE. I've checked the default pages list in IIS, and my Classic ASP homepage still lists as the first selection.
So, I'm kinda confused on how to make MVC and classic asp play nicely together. First and foremost, I'd like to get my dev server pointing back to it's original default page so that it matches the production environment.
Create a separated directory for your MVC4 application beside the web directory. After this create a new web application inside the IIS console and point the source directory to your new application directory (set the new application's AppPool to integrated). Everything should work fine.

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.

Running ASP.NET MVC application in a subdirectory, getting 404 errors

I have an application that uses ASP.NET MVC. I have it deployed to a Crystal Tech server (hence, no direct access to IIS control panel).
Initialy this was running in the root directory and everything was OK. The client then decided that it needed to run in a subdirectory. I moved the app there and the home index page comes up, but every other page tries to access the the controller/action/page/view in the original root directory. I don't understand this, since the references were all contextual (i. e. using ../controller/action as opposed to mysite.com/controller/action).
Am I doing something wrong here? What are my options?
Thanks,
James
I would use the UrlHelper to generate the links. This would ensure that they are relative to the application path.
Link Text
and
<img src="<%= Url.Content( "~/images/myimg.jpg" ) %>" alt="My Image" />

ASP.NET web site on IIS7 on Vista

I have dev an MVC app and deployed it to my local IIS as I am using dev server to dev.
This is on Vista Ultimate.
When i browse the site all the images are not showing and also the
login page is displayed.
what would be causing the images not to show and also why
the login page showed when I have not set up security
in web.config?
I tried to see if the ASPNET account had permissions
but there is user of that name and there is no
Add option in properies either.
Malcolm
This could be a deployment issue, rather than a permissions issue. Did you try to browse directly to an image via your browser?
So if you have an image located in your project as
\images\login.png
open in your browser to:
http://hostname/images/login.png
If this works, then you have got a referencing problem in your html. From memory, most images in asp.net mvc are located with:
src="../../images/login.png"
This could break down if your pathing is different to the current location.
I usually prefer this:
src="/images/login.png"
or even better:
src="<%= ResolveUrl("~/images/login.png")%>"
I had a problem with images displaying in an MVC app until I coded the image tags like this:
<img src="<%= Uri.Content("./content/images/image.png") %>" alt="text" />

Resources