Showing built-in Error View for MVC 3 - asp.net-mvc-3

I followed this tutorial, but I am still receiving the ASP.NET screen that says to turn on Errors do this, or to show custom error page do this.
I have registered the HandleErrorAttribute and added the <customErrors mode="On" /> in web.config. The attribute is sitting directly on the line before the Controller class signature.
Am I still missing something?
EDIT
I removed the attribute from the class as you suggested, and this was the result. Nothing special going on I don't think.
web.config
</appSettings>
<system.web>
<customErrors mode="On" />
<compilation debug="true" targetFramework="4.0">
<assemblies>
Global.asax
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_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
Error*
Server Error in '/' Application.
Runtime Error
Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed.
Details: To enable the details of this specific error message to be viewable on the local server machine, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "RemoteOnly". To enable the details to be viewable on remote machines, please set "mode" to "Off".
<!-- Web.Config Configuration File -->
<configuration>
<system.web>
<customErrors mode="RemoteOnly"/>
</system.web>
</configuration>
Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's <customErrors> configuration tag to point to a custom error page URL.
<!-- Web.Config Configuration File -->
<configuration>
<system.web>
<customErrors mode="On" defaultRedirect="mycustompage.htm"/>
</system.web>
</configuration>

If you wish to see a custom error page (one that you design yourself) then you need to actually create the page and refer to in in the customErrors element;
<customErrors defaultRedirect="GenericError.htm" mode="On" />
In the example above, you would create the GernericError.htm page in your web application. This will be displayed if there is an error.
If you want to see details about the actual exception being thrown, then you need to set the mode to mode="Off" or mode="RemoteOnly"
Also, make sure that you are running the right version of asp.net (i.e. asp.net 4.0) in IIS for your application, otherwise your web.config file may not be parsed correctly, leading to this page.

Here is a conversation about Razor custom-views that works for me and many others. Test it. May be helpful to you too.

Related

How to access web API on client machine

I have hosted web API on Windows server 2012 . I set the binding like port and ipaddress.
After configuration, I browse the API and it's working fine.
Now I wanted to access my configured API from other machines.
What configuration I need to do in my web config.
I am beginner on deployment stuff. Please help me out on this.
Thanks in advance.
You may need to set Access-Control-Allow-Origin headers. Specifically for JSON:
[AllowCrossSiteJson]
public ActionResult YourMethod()
{
return Json("Works better?");
}
Or for a whole controller:
[AllowCrossSiteJson]
public class ValuesController : ApiController
{
You can also edit your web.config to include:
<httpProtocol>
<customHeaders>
<clear />
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
Source: https://learn.microsoft.com/en-us/aspnet/core/security/cors

How to disable custom error page generation in IIS server

My Spring Boot application running on IIS Server generating custom error page for remote request finding error code 401 at header. I want to disable it. There should be no custom error page in response data. Desired behavior is there as default in Apache server. Though IIS Server can be configured to prevent generating custom error page for remote request, I want it to be configured from my application (Web Configuration point may be). Is it possible in Spring?
In your web.xml add this:
<error-page>
<error-code>401</error-code>
<location>/errors/unauthorised</location>
</error-page>
Then add a controller like this:
#Controller
#RequestMapping("/errors")
public class ApplicationExceptionHandler {
#ResponseStatus(HttpStatus.UNAUTHORIZED)
#RequestMapping("unauthorised")
public ModelAndView unauthorizedError(){
return new ModelAndView("errors/error.jsp");
}
}
Add a web.config file to your app's webapp directory (app/src/main/webapp). web.config file should consist:
<configuration>
<system.webServer>
<httpErrors errorMode="Detailed" existingResponse="Auto" />
</system.webServer>
</configuration>
this will cause IIS web server to set error mode to 'Detailed' which will prevent generating custom error page.

CustomErrors not redirecting to custom page but MVC default error page

I have a new MVC 4.0 solution created from scratch using VS2012 web express and IIS Express.
As the tile says I changed my web.config and added the following in :
<customErrors defaultRedirect="Errors/GenericError.html" mode="On">
<error statusCode="404" redirect="Errors/Error404.html"/>
</customErrors>
Basically when I get an exception in my controller the default MVC error.cshtml is showing the error instead of my custom page GenericError.html.
If I go to a URL that doesn't exist, my Error404.html is showing correctly but not for the generic scenario.
Any ideas how I can change this behavior?
Sounds like you don't have the error attribute in your global filters. In an MVC 4 project you should be able to search for the class FilterConfig that was generated for you:
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
}
Update:
The HandleErrorAttribute global filter is for errors that occur in the MVC pipeline, such as errors in your controller as you mention.
The customErrors element in the web.config is for everything else. The HandleErrorAttribute will not honor the value you put in the defaultRedirect attribute of customErrors.
The HandleError filter will look for a shared view which you should have in your generated project shared\Error.cshtml. You can point it to a different view by setting a property on the attribute.
For example, let's say we create a new view under Shared\Errors\CustomError.cshtml, then you could register the filter like this:
filters.Add(new HandleErrorAttribute(){View = "Errors/CustomError"});

CORS support in WebAPI, MVC and IIS with Thinktecture.IdentityModel

I am trying to implement CORS using Thinktecture.IdentityModel as mentioned in the link below:
http://brockallen.com/2012/06/28/cors-support-in-webapi-mvc-and-iis-with-thinktecture-identitymodel/
I tried Web API method and it didn’t work. So I went towards IIS route.
<system.webServer>
<modules runAllManagedModulesForAllRequests=“true“>
<add name=“MvcCorsHttpModule“
type=“Thinktecture.IdentityModel.Http.Cors.Mvc.MvcCorsHttpModule“/>
</modules>
</system.webServer>
And then again in global.asax you would configure the settings:
protected void Application_Start()
{
…
RegisterCors(MvcCorsConfiguration.Configuration);
}
private void RegisterCors(MvcCorsConfiguration corsConfig)
{
corsConfig
.ForResources(“Products.GetProducts”)
.ForOrigins(“http://foo.com”)
.AllowAll();
}
Now, it works in my local host (Windows 8, VS 2012) but when I push it to prod (IIS 6), it doesn’t work. Is there other settings to make it work in IIS 6? Why would it work in the localhost but not when I push it to production.
Your issue is the inverse of this.
Modules are registered differently in IIS6 to IIS7+.
<configuration>
<system.web>
<httpModules>
<add name="MvcCorsHttpModule" type="Thinktecture.IdentityModel.Http.Cors.Mvc.MvcCorsHttpModule"/>
</httpModules>
</system.web>
</configuration>

Is it possible to get standard ASP.NET MVC Unobtrusive Validation to work in Orchard CMS?

I'm trying to build a custom module to integrate with Orchard CMS to implement a business application. While Orchard CMS is an MVC application, it doesn't seem possible (or, at least easy) to do all the things that can be done "out of the box" with MVC.
I'm trying to get unobtrusive validation to work on my view but can't seem to get this to work.
Update: As per Rohan West's advice below, I've now got the scripts included in the page using the ResourceManifest class and the Script.Require calls.
However, the validation attributes on the actual HTML elements are not being generated despite having the .NET attributes on my properties for which I'm using #Html.EditorFor on.
I have set the appSettings in the web.config file as follows:
<appSettings>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
<add key="webpages:Enabled" value="false" />
<add key="log4net.Config" value="Config\log4net.config" />
</appSettings>
Still no joy!
Update 2: As per Rohan West's suggestion, modifying the OrchardStarter class to comment out the following lines "solves" the problem:
ModelValidatorProviders.Providers.Clear();
ModelValidatorProviders.Providers.Add(new LocalizedModelValidatorProvider());
There should be a better way of handling this though.
You need to define the script in the resource manifest for your module.
public class ResourceManifest : IResourceManifestProvider
{
public void BuildManifests(ResourceManifestBuilder builder)
{
var manifest = builder.Add();
manifest.DefineScript("jQueryValidation").SetUrl("jquery.validate.js", "jquery.validate.min.js").SetVersion("1.7").SetDependencies("jQuery");
manifest.DefineScript("jQueryValidation_Unobtrusive").SetUrl("jquery.validate.unobtrusive.js", "jquery.validate.unobtrusive.min.js").SetDependencies("jQuery", "jQueryValidation");
}
}
and then in your page
#{
this.Script.Require("jQueryValidation_Unobtrusive").AtHead();
}
Have a look at the following class
Orchard.Environment.OrchardStarter
In Orchard 1.4.2 there is a line which removes all ModelValidatorProviders
ModelValidatorProviders.Providers.Clear();
This is removing the default DataAnnotationsModelValidatorProvider from the collection.
You could try adding it to the collection,

Resources