asp.net mvc 3 and static resources 404'ing when using "Local IIS Web server" - asp.net-mvc-3

Here's the situation:
ASP.NET MVC 3 application using Razor as the view engine.
Works fine under Visual Studio Development Server (Cassini)
However, when I switch over to "Use Local IIS Web server", the site functions, but every static resource 404s (again, there were no issues under Cassini).
ASP.NET 4.0, Windows 7 Ultimate x64, IIS 7.5, Integrated Pipeline, Network Service as app pool identity.
Specifically, an exception for trying to access a static file that is known to exist (i.e. remove application files, specifically DLLs with route information, etc. and it is served up without issue). Again, this occurs for all static files including i.e. /public/scripts/jquery.js:
Error in Path :/favicon.ico
Raw Url :/favicon.ico
Message :Path '/favicon.ico' was not found.
Source :System.Web
Stack Trace : at System.Web.HttpNotFoundHandler.ProcessRequest(HttpContext context)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
TargetSite :Void ProcessRequest(System.Web.HttpContext) NLogLogger.Fatal => NLogLogger.Fatal => LoggerImpl.Write
I'm baffled. I've verified that a test default ASP.NET MVC 3 app runs fine under both VS Development Server and Local IIS Web server on this machine.
My hope is that someone else has run into a similar issue. In case it helps, here are my routes:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("favicon.ico");
routes.MapRoute(
"Login", // Route name
"login", // URL with parameters
new { controller = "Session", action = "Create" } // Parameter defaults
);
routes.MapRoute(
"Logout", // Route name
"logout", // URL with parameters
new { controller = "Session", action = "Delete" } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}

Of course, it turns out to be something extremely simple: there was an HttpHandler defined in the root Web.config instead of just the View directory's Web.config
<handlers>
<remove name="BlockViewHandler" />
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
Obvious now, but hopefully by posting here it might save somebody else wasting time on something so obvious. See http://haacked.com/archive/2008/06/25/aspnetmvc-block-view-access.aspx for more info.

Related

ASP.NET Core 2.1 MVC, Can we set a restriction to a controller in appsetting.json? WestWind Globalization url

I have a path in my application for handling the string resources on the site. The controller and action are managed by a 3rd party library so I can't really apply to authorize attribute there.
I am using the WestWind Globalization library which makes a URL like https://localhost:44328/LocalizationAdmin/index.html.
Can I restring any controller in my appsetting.json as we do in the web.config in old ASP.NET MVC?
Something similar to below in ASP.NET Core?
<location path="LocalizationAdmin">
<system.web>
<authorization>
<deny users="*">
</authorization>
</system.web>
</location>
Web.config is used by IIS. But ASP.NET Core could be deployed without IIS. When cooperating with Nginx, there's no such way to configure authorization in appsettings.json.
A much more simple approach is to setup a simple middleware:
app.Use(async(ctx , next)=>{
// passby all other requests
if(!ctx.Request.Path.StartsWithSegments("/LocalizationAdmin")){
await next();
}
else {
var user = ctx.User; // now we have the current user
var resource = new { /* ... */ }; // construct description as you like
var authZService = ctx.RequestServices.GetRequiredService<IAuthorizationService>();
var accessible =await authZService.AuthorizeAsync(user, resource,"MyPolicyName");
if(accessible.Succeeded){
await next();
}else{
ctx.Response.StatusCode = 403;
await ctx.Response.WriteAsync("not allowed");
}
}
});

keep getting The view "Error" not found when using Elmah and asp.net mvc 4

I am using Elmah 1.2 as the logging framework for my asp.net mvc 4 application.
in the web.config file, I set customErrors mode to on.
<customErrors mode="On" defaultRedirect="/Error">
<error statusCode="404" redirect="/Error/NotFound" />
</customErrors>
I also created a custom HandleErrorAttribute, copied the code from this link.
http://joel.net/logging-errors-with-elmah-in-asp.net-mvc-3--part-4--handleerrorattribute
In my Home controller, i just throw an exception to test the logging framework.
public ActionResult About()
{
throw new Exception("this is a buggggggggggggg");
ViewBag.Message = "Your app description page.";
return View();
}
"this is a buggggggggggggg" is logged in the database, great, it works. then there's another error also logged, and I didnt expect that to happen.
The view 'Error' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/Home/Error.aspx ~/Views/Home/Error.ascx ~/Views/Shared/Error.aspx ~/Views/Shared/Error.ascx ~/Views/Home/Error.cshtml ~/Views/Home/Error.vbhtml ~/Views/Shared/Error.cshtml ~/Views/Shared/Error.vbhtml
Update:
follow Tim's suggestion, then it causes another issue.
If I create a Error.cshtml in the shared folder. when unhandled exception happens, it will show this Error.cshtml file, not "/Error" page. I have customErrors enabled. They should all get redirected to "/Error" page.
We created an empty MVC5 app and added ELMAH to it. We also were receiving the extra error you described even though we did not add the HandleErrorAttribute. After some research I found the nuget package Elmah.MVC which adds some additional configuration settings. In the appSettings section of web.config you will find these 2 lines:
<appSettings>
<add key="elmah.mvc.disableHandler" value="false" />
<add key="elmah.mvc.disableHandleErrorFilter" value="false" />
</appSettings>
These 2 keys default to "false". I changed their values to "true" and the extra logged exception went away.
I am developing an application using ASP.NET MVC 5 RC and I use Elmah too for error logging. I am using too a custom error handling attribute to redirect errors to a custom action on a custom controller, but mine doesn't look like the one shown in the link you provided.
However I had the same problem: Elmah was properly logging the error, but was also adding a "Error view not found" entry. I solved this by adding the following line to the OnException method on the attribute:
filterContext.ExceptionHandled = true;
For completeness, this is the complete code for the custom error handling attribute I am using:
public class CustomHandleErrorAttribute: HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
filterContext.ExceptionHandled = true;
if(filterContext.HttpContext.Request.IsAjaxRequest()) {
filterContext.HttpContext.Response.StatusCode =
(int)HttpStatusCode.InternalServerError;
filterContext.Result = new ContentResult() {
Content = "Server error",
ContentType = "text/plain"
};
}
else {
filterContext.Result = new RedirectToRouteResult(
"Default",
new System.Web.Routing.RouteValueDictionary(new
{
controller = "Error",
action = "ApplicationError"
}));
}
}
}

ASP.NET MVC Routing - differences in web site vs virtual directory

I have an ASP.NET MVC 3 application (in IIS 7.5) with a portable area. When I host the application consuming the portable area in a web site my routing works perfectly e.g.
http://localhost:9001/Clearance/Home/Search (this works)
However, when hosting in a virtual directory e.g.
http://localhost/Acme.Risks.Clearance.Web.Area.TestUI/Clearance/Home/Search (this does not work)
I get the following error:
Multiple types were found that match the controller named 'Home'. This can happen if the route that services this request ('Clearance/{controller}/{action}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.
I don't understand why, I am specifying namespaces it works fine when running in a web site.
Here is my portable area registration:
public override void RegisterArea(AreaRegistrationContext context, IApplicationBus bus)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
base.RegisterArea(context, bus);
context.MapRoute(
RouteName.ClearanceAreaDefault,
this.AreaName + "/{controller}/{action}/{id}",
new { controller = "Home", action = "Search", id = UrlParameter.Optional },
new[] { typeof(HomeController).Namespace });
}
Here is the Global.asax for the web application in a virtual directory (does not work):
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { typeof(HomeController).Namespace });
}
Here is the Global.asax for the web application in a web site (does work):
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "Acme.Risks.Web.UI.Controllers" });
}
I have used Phil Haack's "RouteDebugger" (http://nuget.org/packages/routedebugger) and the route debug information on both requests is identical.
Does anyone know why this is happening?
Thanks,
Callum
This was down to the namespace used in the data context for MVC.
My web area assembly is "Acme.Risks.Clearance.Web.Area".
My web site assembly is "Acme.Risks.Web.UI" (working).
My web area test UI assembly is "Acme.Risks.Clearance.Web.Area.TestUI" (not working).
The namespace used in the data context was "Acme.Risks.Clearance.Web.Area*", so in the case of the web area test UI the wildcard caused two matches. So even though the routing information was identical, ASP.NET MVC could not decide which was the right assembly because there were duplicate matches.

Configuring IIS 6.0 to run an MVC3 application

Configuring IIS 6.0 to run an MVC3 application
I think I have a configuration issue on my IIS 6 server and I'd like to see if there's anything I've missed.
The problem that I'm having is that anytime when a RedirectToAction("Index", "Home") is executed (e.g. in a method that returns an ActionResult) I would expect that I would be returned to:
http://servername.domain.com/virtualdirectoryname
However, instead I get redirected to:
http://servername.domain.com/virtualdirectoryname/virtualdirectoryname
That is a second instance of the virtualdirectoryname appended to the end of the URL and can't figure out why - this URL will of course yield a 404 resource not found error. I written and deployed several MVC3 applications both in corporate intranet and public internet environments and can't figure out what I've done wrong. My global.asax.cs seems ok -
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
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
);
}
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
if (HttpContext.Current.User != null)
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
if (HttpContext.Current.User.Identity is FormsIdentity)
{
FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
FormsAuthenticationTicket ticket = id.Ticket;
// Get the stored user-data, in this case, our roles
string userData = ticket.UserData;
string[] roles = userData.Split(',');
HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(id, roles);
}
}
}
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
}
The Application_AuthenticateRequest handles the storing of the roles for logged on users, but other than that, it seems pretty vanilla. The only think I can think of is that I've somehow messed up the virtual directory.
Prior to performing any of these steps, I had verified that MVC3 and v4.0 of the .NET framework were installed on the server. There are also other ASP.NET 4.0 applications on this server that have been running without incident. There is also an MVC2 application (MVC2 is also installed) running on this server and has been running without incident.
I created a virtual directory off of the main "default site" using the IIS manager.
Setup appropriate permissions on the folder that this virtual directory points to. Tested with a quick "Hello, World" index.html file.
Copied the application from my development PC where the application works as developed to the folder described in #2.
Updated the Web.Config file, editing the connection strings to point to the test database server; I had also verified these connection strings on my development PC.
Open the web browser and hope for the best.
Any assistance is greatly appreciated.
Thanks!
I think what you may be seeing is:
http://servername.domain.com/virtualdirectoryname/applicationname
If you have named your virtual directory the same name as your application then I could see how that could confuse you. If you had no virtual directory and just your application at the root of the Default Web Site you'd be seeing:
http://servername.domain.com/applicationname
Is your virtual directory the same name as your application name? If so, that is why you see this.

When using MVC3 + SQL CE I get 404 on all pages with scaffolding against the SQL CE Database

The problem only occurs on a server with Windows 2008 Server, locally I run the application and have no issues. I've used the "bin deploy" and "Add Deployable Dependencies..." options and still no luck. Some more context...
The security settings in IIS are set for Windows Authentication, the web.config has a small exclude of anonymous users (not sure this even makes a difference in this scenario).
<authentication mode="Windows" />
In the Global.asax.cs file I have the standard template generated code.
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("elmah.axd");
routes.IgnoreRoute("{*favicon}", new { favicon = #"(.*/)?favicon.ico(/.*)?" });
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
The only addition I've made is for elmah and the favicon. I'm not sure what else to look at from this point forward, so any help would be appreciated.
Also, my connection string to the SQL CE looks like this:
<add name="BillingLogDbEntities" connectionString="metadata=res://*/Models.BillingLog.csdl|res://*/Models.BillingLog.ssdl|res://*/Models.BillingLog.msl;provider=System.Data.SqlServerCe.4.0;provider connection string="Data Source=|DataDirectory|\BillingLogDb.sdf"" providerName="System.Data.EntityClient" />
<add name="BillingLocalDbEntities" connectionString="metadata=res://*/Models.BillingLocal.csdl|res://*/Models.BillingLocal.ssdl|res://*/Models.BillingLocal.msl;provider=System.Data.SqlServerCe.4.0;provider connection string="Data Source=|DataDirectory|\BillingLocalDb.sdf"" providerName="System.Data.EntityClient" />
<add name="OverlayServicesDbEntities" connectionString="metadata=res://*/Models.OverlayServices.csdl|res://*/Models.OverlayServices.ssdl|res://*/Models.OverlayServices.msl;provider=System.Data.SqlServerCe.4.0;provider connection string="Data Source=|DataDirectory|\OverlayServicesDb.sdf"" providerName="System.Data.EntityClient" />
The solution was a combination of two things:
I needed to set the appropriate permissions on the directory that the SQL Server CE files were located inside of.
The Entity Framework needed regenerated to point a the SQL CE databases again. For some reason the application was actually swallowing the errors that were simply a "couldn't connect to the X database" Something in some of the generated code had gotten out of sync.

Resources