URLs pointing to wrong virtual directory on IIS - asp.net-mvc

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))">

Related

How change default url (remove subdirectory) for MVC application

Just deployed ASP.NET MVC5 web application to IIS 10. To do this, under "default website", I created a new application ("Add application"). I had to put in an alias so I put in "xyz".
So, now the public url is similar to http://xyz.mysite.com/xyz .... but I want it to be only http://xyz.mysite.com (remove 'xyz' subdirectory).
How can I do this?
The problem is that my ajax calls are failing because of the subdirectory.
You can do either of the following:
Option 1:
Add Website in your IIS (instead of Add Application) and use the SNI feature. All relative path will be from root (/) level.
or
Option 2:
Modify your AJAX calls to use #Url.Action("actionName", "controllerName") so your URLs will be generated properly based on relative path. I suspect you are hardcoding actionNames to "/controllerName/actionName" which uses root as your relative path.

What determines the current directory in a web application

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.

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 routing, ignore route with extension in the middle of url

Hi How can make the asp.net routing engine ignore routes with an extension of the type
/pathtofile/filename.aspx/morepaths
I know this is hardly a real scenario but I need to know for another similar issue for an autogenerated url
Thanks
The MVC routing engine will not intercept a url if there is a matching file on the file system. (See RouteCollection.Ignore Method) So your example url will work fine. Query strings will also work fine.
You can test this out as follows:
Create an MVC application in Visual Studio
Run it
In your browser enter the url of the Site.css file in the Contents folder.
The file will be served and the browser will pop up the "Save" dialog.
Create an html file anywhere on the site and enter the url.
Your browser will display the html page.
Create an aspx web form anywhere on the site and enter the url.
Your browser will display the web form.
Add a query string or additional path to the url.
Your browser will display the web form.
You can also do this with .asp (classic ASP) pages (although the VS web server won't serve .asp pages, you have to set the site up in IIS for that to work.)
I hope that answers your question.

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" />

Resources