3

I'm developing an ASP.NET MVC 5 app with .NET Framework 4.5.1 and C#.

I want to redirect to home/index when an user tries to get access to a resource that doesn't exist.

Yes, there are a lot of questions about how to solve this problem. I read them, but their solutions don't work for me.

Now, I'm trying this on web.config:

<system.webServer> <httpErrors> <remove statusCode="404" subStatusCode="-1" /> <error statusCode="404" path="~/Home/Index" responseMode="Redirect" /> </httpErrors> </system.webServer> 

But, when I try to access this URL, http://host01/Views/ConnectBatch/Create.cshtml, I get the default 404 page.

Maybe the problem is in path="~/Home/Index" but I tried with path="~/" and I get the same default 404 page.

Any idea?

3
  • 1
    Error filters... global filters. That kind of thing Commented Jul 14, 2015 at 7:30
  • Just to eliminate the obvious: you're using IIS/IIS Express, and not Cassini? Commented Jul 14, 2015 at 7:34
  • I'm also surprised you're getting a 404 rather than a 403; directory browsing is supposed to be forbidden by default in an ASP.NET MVC project. Commented Jul 14, 2015 at 7:39

5 Answers 5

3

There lot of thread in stackoverflow but my case ..working this

<customErrors mode="On" > <error statusCode="404" redirect="/404.shtml" /> </customErrors> 

Orginal Thread

Or

You Should Handle The error in Code behind Like this (global.asax)

public class MvcApplication : HttpApplication { protected void Application_EndRequest() { if (Context.Response.StatusCode == 404) { Response.Clear(); var routedata= new RouteData(); routedata.DataTokens["area"] = "ErrorArea"; // In case controller is in another area routedata.Values["controller"] = "Errors"; routedata.Values["action"] = "NotFound"; IController c = new ErrorsController(); c.Execute(new RequestContext(new HttpContextWrapper(Context), routedata)); } } } 

In your error Controller Like this

public sealed class ErrorsController : Controller { public ActionResult NotFound() { ActionResult result; object model = Request.Url.PathAndQuery; if (!Request.IsAjaxRequest()) result = View(model); else return RedirectToAction("Index", "HomeController"); } } 

Orginal Thread

Sign up to request clarification or add additional context in comments.

Comments

2

This is how I have solved my problem. Maybe others answers can solved it but I have tested a few of them (I have added a comment on answers that I've tested saying that, that answer doesn't work for me).

I have added this in Web.config inside <system.web>:

<customErrors mode="On" redirectMode="ResponseRedirect"> <error statusCode="404" redirect="~/"/> </customErrors> 

I want to redirect to /Home/Index when an user doesn't find a resource.

Comments

0

You could try ~/ or ~/home/index. Or a long shot would be changing the web.config in the views folder

update Or maybe this answer is the way to go, my first instinct when I read your question was a handler

Comments

0

Well you can try to use :

<httpErrors existingResponse="PassThrough" /> 

From : ASP.NET MVC 404 handling and IIS7 <httpErrors>

And That my Help :

ASP.NET MVC 404 Error Handling

Comments

0

Try this, using Session variables... initialize Session variable when the resource is available

for example:

Session["user_name"] = resource.name; 

Check this variable in the page-load function

if(Session["user_name"] == null) Respon.Redirect("login.aspx"); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.