asp.net mvc route conflicting with areas - asp.net-mvc-4

Our asp.net mvc 4 application has a route defined as following
routes.MapCustomRoute(
name: "Startup",
url: "{area1}/{param1}/{param2}/{param3}",
defaults: new { controller = "Startup", action = "Index", param3 = UrlParameter.Optional },
namespaces: new[] { "namespaceofmaincontrollers" }
);
We also have an area with the name "area1". We are getting http 404 error browsing to the route.
We have tried using routes.RouteExistingFiles = true; to no avail.
We want to hit the controller action Index of the StartupController defined in the main portion of our application .
Any help will be greatly appreciated.

We figured out that while area registration the namespaces were incorrect which was the cause of the problem. Once corrected our 404 errors disappeared.

Related

Hosting MVC 4 Application in IIS Application

I have several applications deployed to IIS. I created a site for each application and mapped them using different port number. Recently, I was asked to use virtual directory instead of mapping them using different port number. I created the virtual directory and add a route for it. When I tried to test the application locally, I was getting a 403.14. After reading several post online, I made the following changes to my web.config file
<modules>
<remove name="UrlRoutingModule-4.0"/>
<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule,
System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
preCondition="" />
</modules>
I am not getting a directory with all the files name. I updated my route in the
RouteConfig.cs file to
routes.MapRoute(
name: "Default",
url: "CollegeOfBusiness/{controller}/{action}/{id}",
defaults:
new { controller = "Account", action = "Logon", id = UrlParameter.Optional},
namespaces: new[] { "MyCollege.Controllers" }
);
After making those changes, I am not getting the 400.13 anymore; however, I am not getting the login page. I am getting a directory of all the files. I ran the command "aspnet_regiis /i" and then aspnet_regiis -ir to make sure that it was not a registration issue. I have not had any luck so far. I am looking for any information or resources that could help fix this issue.
Local System:
Windows 7
IIS 7.5
Visual Studio 2017
Asp.net MVC 4
Jquery 2.3
Update 07/03/2018 #9:45
I modified the route as shown below. I am not getting the directory listing anymore. However, POSTs and GETs are not routing to the appropriate controller.
The url when the page first loaded looked as shown here:
http://localhost/CollegeOfBusiness
I added the custom route below. It is added right above the default route.
RouteConfig.cs file to
routes.MapRoute(
name: "CollegeOfBusiness",
url: "CollegeOfBusiness/{controller}/{action}/{id}",
defaults:
new { controller = "Account", action = "Logon", id = UrlParameter.Optional},
namespaces: new[] { "MyCollege.Controllers" }
);
Then, I add the default route at the bottom.
RouteConfig.cs file to
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults:
new { controller = "Account", action = "Logon", id = UrlParameter.Optional},
namespaces: new[] { "MyCollege.Controllers" }
);
When the user clicked on login button, the post URL looked as follow:
/CollegeOfBusiness/Account/Logon
Finally, I was able to get it to work.
1. I removed the below custom route because it was not needed.
RouteConfig.cs file to
routes.MapRoute(
name: "CollegeOfBusiness",
url: "{controller}/{action}/{id}",
defaults:
new { controller = "Account", action = "Logon", id = UrlParameter.Optional},
namespaces: new[] { "MyCollege.Controllers" }
);
As suggested in this POST, I added the url to a div and get it based on the Id. I also change the src for all js and css file to use #Url.Action().
I had to prepend '/CollegeOfBusiness' in front of all requests
I changed the application pool to use a service account. This account did not have access to update the database, so I fixed that as well.

Overriding Controller Routing

I'm using ASP.NET MVC 5 and I'm looking to dynamically choose which controller handles a particular request.
For example, the user might request /ControllerName/ActionName/, but I want to first look and see if in my source I have a controller /CustomCodeDirecotry/ControllerName/ActionName. If I have that controller, then use that to process the request. If I don't then go ahead and use the requested controller.
The key in this is that I don't want the user to know that they were handed off to a different url.
In the end the answer to this question was to provide a namespace when mapping routes. Specifically:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MvcSite.Special.Controllers" });
I found my answer here

MVC routing under a domain

I have just published an MVC application. It has a Home controller with a method Create.
The URL is http://www.myurl.com
The name of the virtual directory in IIS is myurl.
When I submit the form that posts to Home/Create I'm getting an error because it's trying to send the data to:
http://www.myurl.com/myurl/Home/Create
rather than:
http://www.myurl.com/Home/Create
I tried adding this route:
routes.MapRoute(
"test",
"Home/CreateNewPlayer",
new { controller = "Home", action = "CreateNewPlayer" }
);
but that hasn't made any difference. Can someone please help me out?
You should not host your application in a virtual directory but rather directly inside the website which has a binding to the www.myurl.com domain.

MVC4 - My Custom Route isn't working

So I have a custom route in my MVC4 web app below:
routes.MapRoute(
name: "SecondarySportsCategoryLanding",
url: "sports/{name}/{id}",
defaults: new { controller = "FrontCategoriesController", action = "Sports", name = UrlParameter.Optional, id = UrlParameter.Optional }
);
I have that placed above my default one as well. What's happening is when I go to /sports I get this error:
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.
Requested URL: /sports
I have the controller specified, and inside that I have an Action titled Sports with 2 optional parameters (defaulted to nulls). Any ideas?
controller = "FrontCategoriesController" should be controller = "FrontCategories"
Because the framework automatically inserts Controller into the value for you.

MVC2 Routing with WCF ServiceRoute: Html.ActionLink rendering incorrect links!

I have a WCF service that lives side-by-side with an MVC2 web site. I'd like for my URL for the service to look like this:
http://localhost/projdir/Service
The MVC site is in its infancy so it still has all its boilerplate controllers etc.
The following code works at first glance in global.asax:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(new ServiceRoute("Service", new ServiceHostFactory(),
typeof(MyService)));
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index",
id = UrlParameter.Optional } // Parameter defaults
);
}
The service appears just where I described and works as advertised. Great.
However, I just noticed that ordering my code this way changes all of my ActionLink's. For example, the "About" tab on the MVC site now appears like this:
http://localhost/projdir/Service?action=About&controller=Home
This is obviously incorrect (it should be http://localhost/projdir/Home/About/ ).
If I move the ServiceRoute addition below the default MapRoute() call, then I get a missing controller error. (Actually I get a "StructureMapControllerFactory did not return an instance of a controller" error, because I'm wired up with StructureMap, duh, it's not a controller to begin with.)
Interestingly, it only seems to be affecting the output of Html.ActionLink(). I can manually type in http://localhost/projdir/Home/About/ and get to the correct page.
What horribly obvious newbie mistake am I making?
Try moving the Service route after the MVC route. But to avoid the "missing controller" error that you got before, add the MVC route with a Route Constraint. These route constraints can be Regex - basically you'd want your route constraint to be any controller that is not "Service". When a request for "Service" is requested, it will make it fall through and his the WCF Service Route.
I resolved with that:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { controller = "^(?!api).*" }
);
routes.Add(new ServiceRoute("api", new DataServiceHostFactory(), typeof(dwService)));
I hope this good for you
Another solution is to inherit the ServiceRoute and override the GetVirtualPath method to return null as described here
public class AppServiceRoute : ServiceRoute
{
public AppServiceRoute(string routePrefix, ServiceHostFactoryBase serviceHostFactory, Type serviceType)
: base(routePrefix, serviceHostFactory, serviceType)
{
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
return null;
}
}
This way, reverse route mapping never select this route for any Action. Works like a charm