IIS 8.5 MVC 5 Routes 'api/controllerName' gives 404 Not Found Error - asp.net-mvc-5

Within a MVC 5 Webapi project, I am trying to do a GET request via jquery using URL 'api/controllerName'. The web application is hosted under Default Website and the root of the webisite is "localhost/myApp".
The route 'api/controllerName' gets translated to 'localhost/api/controllerName' instead of 'localhost/myApp/api/controllerName' throwing 404 Not found Error.
This has to do something with IIS 8.5 and MVC 5 routing, because:
The Same setup works well on IIS 7.5
The same set up works well on IIS 8.5 if i do not use a virtual directory and host the application directly at Default Web Site.
My Global.asax.cs's RegisterRoutes Method looks like below
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}

I figured this out. Apparently, IIS 7.5 worked fine with the bare route 'api/controllerName' but IIS 8.5 needed a fully qualified route created using #Url.RouteURL utility with RouteName as "DefaultApi".
i replaced the URL with
'#Url.RouteUrl("DefaultApi", new { httproute = "", controller = "controllerName"})'
and it worked like a charm (on both IIS 7.5 and IIS 8.5).

Related

ASP.NET Core 6 MVC published to local IIS routing not working HTTP 500 error

I have published an ASP.NET Core 6 MVC web application to my local IIS. The site loads without error, however the routing doesn't work and I am left with a HTTP 500 error when navigating from the home page.
The routing works when I build and load directly from Visual Studio. I am not sure what elements you would need to help me with this, but here is my program.cs.
Help please :). Richard
using Microsoft.EntityFrameworkCore;
using RabsoInc.Web.DAL;
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
ConfigurationManager configuration = builder.Configuration;
builder.Services.AddMvc();
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(configuration.GetConnectionString("default")));
builder.Services.AddTransient<IDomain, DomainRepo>();
builder.Services.AddTransient<IItem, ItemRepo>();
var app = builder.Build();
app.UseRouting();
app.UseStaticFiles();
app.UseDefaultFiles();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
app.Run();

DNS and MVC 5 routing (Internet vs Internal Lan)

I know there are many helps on MVC 5, but I have not seen the following question answered.
My application works fine on the local LAN... I can access using expected URL:
http://www.mycompany.com/myapp
However when I try to access the server over the INTERNET I can't access the application via the hostname, only via the external IP address:
http://xx.xx.xx.xx/myapp
I have talked to the firewall / DNS admin people and the assure me that it is an application problem. We are using GoDaddy to handle DNS. The admin has setup and A record to point to the server, but after working with GoDaddy they have setup subdomains as the way to access applications...
They have another Internet application (that uses asp.net) working using the following URL:
http://appname.companyName.com
This is the way they expect my application to route on the Internet...
I am using just the standard default MVC routes... since my application routes correctly on the local lan I assumed the problem is with the DNS ... and the admin people say it is an MVC problem...
So what is the problem and what is the solution? Are the DNS / Firewall admin poeple right? Is this an issue caused by my MVC routing?
Here is my standard RouteConfig.cs file... I don't have anything special setup...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace EtracsWeb
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}

ServiceStack renders Snapshot views instead of Razor views when deployed on IIS unless fullpath to views is specified in DefaultView

If I specify DefaultView like this, it works on my local IIS express, but not when deployed to IIS:
[DefaultView("Login.cshtml")]
public class SiteLoginService : EnshareServiceBase
{
}
My Views folder contains Login.cshtml.
It works as expected when running locally on IIS express, but will render the snapshot view when deployed on IIS.
After changing to full path, it works also in IIS:
[DefaultView("/Views/Login.cshtml")]
public class SiteLoginService : EnshareServiceBase
{
Is this by design or is there any other way how to configure ServiceStack to look into the Views folder also when deployed in IIS?

Add Static HTML file to Root of Azure MVC App

Where do I add a static html file "mycoolpage.html" to a MVC4 project so it will be served when requesting "mysite.cloudapp.net/mycoolpage.html" when deployed to azure?
I've tried adding it to the project and setting up routes like
routes.IgnoreRoute("{resource}.html");
and tried returning it from a controller using the following route
routes.MapRoute(
"mycoolsite.html",
"mycoolsite.html",
new { controller = "Home", action = "Coolsite" }
);
Have you tried: routes.IgnoreRoute("*.html"), with the file in the root folder of your MVC site?
(From: Getting MVC to ignore route to site root)

mvc3 on iis6 , only home page works , routes gives 404

I've deployed a MVC3 application to a win2003 server with .Net4.0 installed . I've configured wildcard mapping for application as described here . Also i did it before for other mvc3 applications on the same server before . I'm sure that the IIS configurations of both mvc3 applications are same.
When i hit the default home page of the default route, it works, but other controllers and actions gives 404. I can't figure out a way for 2 days . Any help will be great.
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 );
Make sure that ASP.NET 4.0 is properly registered with IIS 6.0
c:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i
and that it is enabled:
Verify below items in IIS
Home Directory tab -> Configuration. Under Wildcard application maps,
1.the entry aspnet_isapi.dll has been added
2."verify file exists" checkbox should be in unchecked state

Resources