Custom 404 page for invalid request - asp.net-mvc-4

I have implemented custom error pages in an MVC4 application. Basically I've added the following to my Web.Config file:
<customErrors mode="On" defaultRedirect="Error">
<error statusCode="404" redirect="~/Error/NotFound"/>
<error statusCode="500" redirect="~/Error/Index"/>
</customErrors>
Unfortunately, if someone uses a request that adds a path to a route the redirect doesn't take place. For example I have a Document method that takes an ID on the controller Content. The following request is valid:
/Content/Document/1
I get the 404 displayed when someone calls an url like this:
/BlaBla
/Content/BlaBla
Some users manage to add /BlaBla after the ID:
/Content/Document/1/Blabla
This is the case where my custom 404 page is not shown. How can I handle this?

A CatchAll - Route solved the problem. A question with an answer which made me find the solution:
MVC 4 catch all route never reached

Related

Unable to redirect to error page in blazor server as it is returning 200 status code to the page that does not exists

In Blazor, we can directly assign the route to each page on the razor itself. (that's the fun part doing) but I want to navigate to "/error" or "/404" page when any non-existing url is entered. Please Help me how we can get 404 status and implement the redirection. as it is an important task for SEO purpose.
ive tried many solutions: 1: i have created a component "error.razor" and added that component in app.razor inside the notfound section. in this scenario i am getting a 200 status code with same non-existing url in address bar
2: for a forceful redirection, i have injected NavigationManager in the "error.razor", with the help of NavigationManager i am redirecting it to 404 page. and i.e. a 302 redirection.
Edit: I am trying to get 400 status code on non-existing page. but i am getting 200. how can i get proper 400 error status in network panel. Help me on that.
FYR :
Correct url
Damaged Url/Non Existing URL
help me out if you have any solution.
You can write a custom NotFoundPage component that will display what you want the user to see when they navigate to a page that does not exist and put it under NotFound Context in app.razor
<Router>
<NotFound>
<CascadingAuthenticationState>
<NotFoundPage></NotFoundPage>
</CascadingAuthenticationState>
<NotFound>
</Router>

What is the right way to handle error pages in moqui

To redirect user to an error page I am adding entry in web.xml for 401 (Unauthorized) error code. But I don't find it correct. As, I am adding following code in web.xml which is in framework.
<error-page>
<error-code>401</error-code>
<location>/myapp/Error/Error401.html</location>
</error-page>
And, What if there are more than one component in my application and I want to redirect on different error pages on same error code as per component. Please guide me as to how can I achieve the same?
Thanks in advance.

Getting routes to handle static content types that produce 404s

I have an MVC site which is replacing an old website. I want to be able to handle requests for the old static html files but redirect to the 404 error handler on my MVC site - I'd also like to provide a 301 code instead of a 404 to let the crawlers know that the content has moved
At the moment when I try and navigate to static content I get the IIS 404 error. I can use the custom error handling to handle the missing file but I'm not sure I like the way this is working
<httpErrors errorMode="Custom">
<remove statusCode="404"/>
<error statusCode="404" path="http://localhost/site/Error/NotFound" responseMode="Redirect"/>
</httpErrors>
Is there a better way to do this or is this the way that everyone else is doing it? How do I provide a 301 instead of a 404 since it's the static file handler that's serving the content?
Is there a way to get MVC to handle requests for certain file extensions and catch 404s from these if they don't exist?

url with extension not getting handled by routing

I've been folowing the advice from this article for setting up a robots.txt file in asp.net mvc3 for using a controller to handle the server response, and IIS 8.0 express is returning a file not found error, rather than an asp.net error.
How do I get IIS to not look for a file in these cases? Is there something I need in the web.config?
IIS tries to be intelligent here. He intercepts the dot in the url and thinks that this is a static file and attempts to serve it with the default StaticFile handler. it dopesn't event get to the managed ASP.NET application.
The first possibility is to add the following in your web.config
<system.webserver>
<modules runAllManagedModulesForAllRequests="true" />
but actually that's not something I would recommend you doing because this might have a negative effect on the performance of your application because now all requests to static files (such as .js, .css, images, ...) will go through the managed pipeline.
The recommended approach is to add the following handler to your web.config (<handlers> tag of <system.webServer>):
<system.webServer>
<handlers>
<add name="Robots-ISAPI-Integrated-4.0" path="/robots.txt" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
...
</handlers>
</system.webServer>
Notice how we have specified that this handler will only apply to a particular URL and HTTP verb.
Now when you GET /robots.txt, IIS will no longer handle it with the StaticFile handler but will instead pass it to the managed pipeline ASP.NET. And then it will be intercepted by the routing engine and routed to the corresponding controller action.
Unless you need a dynamically generated robots.txt file, which is very rarely necessary, just do the following:
Ignore the route to robots.txt
routes.IgnoreRoute("robots.txt");
Add the robots.txt file to your root dir

Custom error pages shown using IIS6 rather than web.config settings

this is my first post on this great source of programming information.
I have developed new site for client and just doing some finishing touches.
I am trying to create custom error pages which would be read from web.config
<system.web>
<customErrors mode="RemoteOnly">
<error statusCode="404" redirect="404.aspx" />
<error statusCode="500" redirect="500.aspx" />
</customErrors>
It works well on local development machine and 404 and 500 errors are shown as required.
After compilation and publishing site to web server it doesnt work. IIS 6 keeps on showing original IIS 6 error pages like this 404 error:
The page cannot be found
The page you are looking for might have been removed,
had its name changed, or is temporarily unavailable.
Please try the following:
Make sure that the Web site address displayed in the address bar of
your browser is spelled and formatted correctly.
If you reached this page by clicking a link, contact the Web site
administrator to alert them that the link is incorrectly formatted.
Click the Back button to try another link.
HTTP Error 404 - File or directory not found.
Internet Information Services (IIS)
I am not sure why is it doing this, I thought that web.config overwrites original IIS6 settings.
Ok I have found out that the problem is with the 404 .aspx page only. Error 500 is working fine and showing 500.aspx page. But not for 404.aspx. Please advice
The web config will only override IIS6 settings if the request is passed to the .NET ISAPI filter - for example, for pages ending with .aspx
If I browsed to a file or directory that didn't get to the .NET ISAPI it wouldn't use the web.config rules.