Remove application name from url in ASP.NET MVC application - asp.net-mvc

In my development environment
Url.Action("Index", "User") ---> /user
In production, the application is configured under a application named "ucms"
Url.Action("Index", "User") ---> /ucms/User
I have authorization based on the url i.e., /user, so it is failing in production environment. How do I fix this issue to remove ucms?
Edit
Routes are default one's. FYI, I've upgraded application from mvc 3.0 to 4.0.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Edit
I have figured out one way to do it, could anyone share thoughts on this.
Store the virtual path in web.config.
<add key="appvirtualpath" value="~/ucms"/>
And while passing the url to the database layer, I would replace the virtual path will blank.
Url.Action("Index","User").Replace(ConfigurationManager.AppSettings["appvirtualpath"].ToString(),
"~");

I solved this problem by adding a virtual path in development environment
Right click on project, properties, web and then virtual path.
I don't know if there is a way to configure it in the production environment

Related

Is possible to publish web application mvc 4 (template 4.5.2) on iis7 using file system as publishing method since vs 2015?

The application in local environment runs well, but when it's published
on live web server displays
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
I solved the problem. Yes, it is possible.
However the default page is not defined in web.config. It must be specified in RouteConfig.cs inside of App_Start directory. Something like this:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
As you can see, the absolute path is without cshtml extension, so could be put in url browser something like:
http://server:prort/Account/Login

ASP .NET MVC RedirectoToAction ignores the application where is located

I have a MVC application deployed in my server in a virtual directory like:
http://localhost/myapp/
Where "myapp" is the virtual directory
In my Login view, located in
"http://localhost/myapp/user/login",
I redirect to the index using RedirectToAction("Index", "Home"), it seems the application try to redirect to
"http://localhost/home/index"
instead of
"http://localhost/myapp/home/index".
The application works when it's located in the root of the IIS Web Site but doesn't work in the given situation.
It's there a way to configure the application root, that I missed?
Settings: Microsoft Visual Studio Express 2012 for Web, IIS 7 under Windows 7 , application pool ASP .NET v4.0
I am 99% positive that doing:
return RedirectToAction("Index", "Home")
is application root relative meaning that it should redirect to the application you're in regardless of the virtual directory settings or where the application is located. I mean think of the nightmare otherwise that every time you move the app to a different virtual directory you need to update the global.asax or web.config file??? Ridiculous! We have the same setup that you have and we have no issues with "app hopping."
Are you sure that RedirectToAction is causing this? Could it be that you have something like:
#Url.Content("/Home/Index")
In that case you would experience this issue and you can easily fix this by doing:
#Url.Content("~/Home/Index")
~ symbol makes it application root relative...
thats correct functionality. MVC will calculate the route as controller/action by default.
If you want this to do otherwise you need to add a route into the Global.asax:
//this is your new route which needs to be ABOVE the detault
routes.MapRoute(
// Route name
"myapp_Default",
// Url with parameters
"myapp/{controller}/{action}/{id}",
// Parameter defaults
new { action = "index", id = UrlParameter.Optional });
//This is the default route that should already be there.
routes.MapRoute(
// Route name
"Default",
// Url with parameters
"{controller}/{action}/{id}",
// Parameter defaults
new { action = "index", id = UrlParameter.Optional });
more info on routing in scott gu's blog

ASP.NET MVC2 Routing IIS6 - only default routes work

I've deployed an ASP.NET MVC2 website on a Windows Server 2003 machine running IIS 6.
I'm using pretty much the default routing in a standard MVC project:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Products", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
}
Navigating to http://localhost/MyApplication takes me to the List page just fine. Navigating to http://localhost/MyApplication/Products/Details/21 gives me a 404. This routing worked fine on the inbuilt development server in VS2010.
I've put in the standard IIS wildcard aspnet_isapi.dll mapping mentioned all over the place - navigating to the List page didn't work before I did - but navigating to anything other than the default route is broken.
I'd really like to keep my extensionless URLs. Does anyone have any idea as to why the routing would work for the default webpage, but no others?
*Edit: just tried adding the .aspx extension, ie now my route looks like this:
routes.MapRoute(
"Default", // Route name
"{controller}.aspx/{action}/{id}", // URL with parameters
new { controller = "Downtime", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
And it has the same behaviour, except this time I get the asp 404 page instead of the html 404 page...
*Edit 2: tried it again using the following route and making sure .mvc was mapped to aspnet_isapi.dll:
routes.MapRoute(
"Default", // Route name
"{controller}.mvc/{action}/{id}", // URL with parameters
new { controller = "Downtime", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
This time I got an 'Internet Explorer cannot display the webpage' 404-style page. I've now got 3 different 404 errors from using these 3 different methods...
*Edit 3 Return of the Edit: I have the site running in IIS 5.1 on Windows XP professional with only a wildcard remapping on the virtual directory, but heaven forbid I can get it to run on the webserver...
First of all, you should follow this walk-through: http://haacked.com/archive/2008/11/26/asp.net-mvc-on-iis-6-walkthrough.aspx
(just to make sure you've not missed something). This shows how to get the extension-less and extention'd versions working. You might want to at least check whether the extension'd version works to check that other things aren't misconfigured.
After that, perhaps you should try adding something like this..
http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
... as it can really help in determining where your routes are broken.
Some other things to check:
Is IIS configured with ASP.NET? Check properties of the virtual directory. On the VDir select the configuration button and check that the usual ASP.NET extensions are mapped to the .NET 2.0 ISAPI dll. This will be something along the lines of
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll.
Is ASP.NET 2 enabled on IIS (under IIS 6 this is off by default): in IIS manager check the Web Service Extensions folder to enable it.
Have you deployed global.asax?
Found it - the routing is actually working, my URLs were not. I was using javascript to build some of the URLs and it turns out that I wasn't linking to
http: //localhost/MyApplication/Controller/Action/ID
I was actually linking to
http: //localhost/Controller/Action/ID
Building the links like this was working on the development server, but once the site was deployed to a virtual directory on the webserver those addresses are incorrect because of the extra application name in the URL.
In conclusion, be careful with your URLs - don't build them out of strings like I did.

Routing in ASP.NET MVC

I want to identify categories in my site in url not by id, but by its name. When i'm adding category, which name contains "+" symbol - i have 404 error. This situation is on product internet server, when i'm deploying on local visual studio server - all work fine. Please, suggest me smth.
Example:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Catalog", id = "" }
);
http://wazy.ru/Catalog/Category/18+
Server wont throw 404 error when category names wrong. I think IIS cannot reach-invoke controller or action. Maybe this link help to figure out how to deploy.

ASP.NET MVC - Running in a Subdirectory

I am attempting to deploy an ASP.NET MVC application in a subdirectory of an existing application and I am running into some routing issues. I have set up the folder structure such that all of the binaries and config files for the MVC app are correctly located in the root directory, while the rest of the content is in the subdirectory. Additionally, I updated all of the routes in the MVC application to reflect the subdirectory; however, every request to the application produces:
The incoming request does not match
any route.
All defined routes are being ignored, including the default route:
routes.MapRouteLowercase(
"Main_Default",
"blog/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
I tried enabling RouteDebug to test the issue, but even that is not getting routed to. Any advice on what else I can try?
Note: This question is not a duplicate.
Try running it as a virtual directory instead of just a directory, otherwise your routes are not going to be called. You will not need to put the name of the virtual directory in the route.
Here's a route that I have setup in a v-dir MVC app that works just fine...
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Tour", action = "Index", id = "" } // Parameter defaults
);
Looks like I found the problem.
In addition to the binaries and the config files, Global.asax must also be placed in the root in order for its code to be executed.
Thanks guys. :)

Resources