Reputation: 45921
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?
Upvotes: 3
Views: 6222
Reputation: 1316
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");
Upvotes: 0
Reputation: 8285
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
Upvotes: 3
Reputation: 45921
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.
Upvotes: 2
Reputation: 329
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
Upvotes: 0
Reputation: 2786
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
Upvotes: 0