asp.net mvc using HREF in application running on IIS - asp.net-mvc

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.

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.

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

URL Routes In IIS7 MVC 5 (Single Paged Application)

After publishing a MVC5 web application of mine to my IIS server (Individual User Accounts), it would seem that the URL is accessed incorrectly.
During debug, it would be e.g http://localhost:1234/api/Account/UserInfo?=XXXXX
The debug works just fine. The only issue kicks in after I've published it via my IIS7 server.
After publishing and using Google Chrome's console, it would appear that the page is requesting for a resource at mydomainname.com/api/Account/UserInfo?=XXXX instead of mydomainname.com/WEBAPPLICATIONNAME/api/Account/UserInfo?=XXXX.
My best guess is to modify the URLs in /Scripts/app/app.datamodel.js but it would just cause more parsing problems.
I've searched around and can't seem to find any related problems. I hope someone here will be able to lend a hand.
Look like you are using relative path like "/api/Account/UserInfo". Instead i'll recommend you to use #Url.Content("/api/Account/UserInfo"). This will solve your problem
Explanation
In local system when we run application in WebDev server it never have sub folder (like WEBAPPLICATIONNAME) therefore you relative path work correctly. but when you host your application in IIS under Default website in another new website /Virtual folder (like 'WEBAPPLICATIONNAME') then "/api/Account/UserInfo" fall back to Default Website because for '/' in starting. #Url.Content or #Url.Action make sure to add virtual directory name, hence changing your path to "/WEBAPPLICATIONNAME/api/Account/UserInfo" in IIS.

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 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.

Resources