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?

VansFannel
  • 45,055
  • 107
  • 359
  • 626

5 Answers5

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

illeb
  • 2,942
  • 1
  • 21
  • 35
Jagadeesh Govindaraj
  • 6,977
  • 6
  • 32
  • 52
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.

VansFannel
  • 45,055
  • 107
  • 359
  • 626
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

Community
  • 1
  • 1
matt_lethargic
  • 2,706
  • 1
  • 18
  • 33
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

Community
  • 1
  • 1
Nikolay
  • 329
  • 1
  • 7
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");
Sikandar Tariq
  • 1,296
  • 1
  • 13
  • 29