Hosting MVC 4 Application in IIS Application - asp.net-mvc-4

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.

Related

Error enabling swagger with swashbucle

I am trying to create a WebApi project with a couple of simple controllers. If I call the methods using fiddler all is fine, however I prefer to use swashbuckle as it's a bit prettier.
However, with swashbuckle installed using the default configuration it isn't working.
When I navigate to
http://localhost/api/mycontroller/swagger
it redirects to
http://localhost/api/mycontroller/swagger/ui/index
But then it just displays the following error:
<Error>
<Message>
No HTTP resource was found that matches the request URI 'http://localhost/api/Management/swagger/ui/index'.
</Message>
<MessageDetail>
No type was found that matches the controller named 'swagger'.
</MessageDetail>
</Error>
My routing is as follows:
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
We found a very simple solution to our issue.
When swagger was added to the project it automatically updated the Bootstrapper.cs within App_Start, and it looked like this:
WebApiConfig.Register(config);
UnityConfig.Register(config, container);
SignalRConfig.Register(app, container);
SwaggerConfig.Register(config);
Simply moving swagger to the top of the list allowed it to register it's routes correctly before the web api routes:
SwaggerConfig.Register(config); // Swagger must be the first thing in this sequence or it just does not work...
WebApiConfig.Register(config);
UnityConfig.Register(config, container);
SignalRConfig.Register(app, container);
Just posting this in the hope that it helps someone else.
I had the same issue, we are only using attribute routing so this may not apply to everyone.
By default the WebApiConfig looks like this:
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
If you remove the section for to MapHttpRoute for the default routing and just have:
config.MapHttpAttributeRoutes();
It works like a champ!!! I tried following the FAQ regarding fixing it but this was the only thing that worked. Also another item to consider is that I only needed Swashbuckle.Core.

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.

Convert ASP.Net MVC4 site to Azure

I've created a ASP.Net MVC 4 web site that has several custom routes which are registered in the ~/App_Start/RouteConfig file.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Empty",
url: "",
defaults: new { controller = "Home", action = "Index", id = 0 }
);
routes.MapRoute(
name: "Session",
url: "{id}",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
This site is working great outside of Azure and even works in the Azure emulator, but when I move it up to Azure, it's acting like there are no routes. For instance, when I try to navigate to the root of the web site I get an error stating:
403 - Forbidden: Access is denied.
You do not have permission to view this directory or page using the credentials that you supplied.
This made me think that the route simply wasn't working so I tried going to a another page like http://myapp.cloudapp.net/1234 (which should send 1234 into the home controller's index action. But that action gives me a 404 error.
I've checked my configuration against and new ASP.Net MVC 4 Azure project and they appear to be the same. I've also read through this which gave some pointers but ultimately didn't help.
Any advice on how to troubleshoot this?
Update
This is a WebRole project and is using .Net 4.0 (I already down converted from 4.5).
Have you tried setting a default page as per this article http://www.sunilhaste.com/2011/04/403-forbidden-access-is-denied-error.html
After you have uploaded your Web Role to Azure, you try to access the application and suddenly you get "403-Forbidden, Access denied"
error.
You are able access a page by providing it's complete url ex :- http://xyz.cloudapp.net/Pages/Home.aspx.
You don't have a default.aspx page present at the root level of your website. Solution :- The first thing that you should do is, to
check whether you have added below tag to the web.config file inside
system.webserver tag or not.
<add value="Pages/Home.aspx"/>
I also had had a similar issue and mine was fixed by setting the role to be 2008 R2. Someone in comments to the linked article says
change OS CONFIGURATION from MS SERVER 2008 SP2 to MS SERVER 2008 R2
Also see the following Asp.net and Azure - Can't no longer show my web application suggests checking you have <modules runAllManagedModulesForAllRequests="true" />
Also I assume you are making sure that MVC4 files are being deployed to Azure. Have your RDP'd to the machine and checked event log etc?