404时重定向到home / index

时间:2015-07-14 07:21:38

标签: c# asp.net asp.net-mvc asp.net-mvc-5

我正在使用.NET Framework 4.5.1和C#开发ASP.NET MVC 5应用程序。

当用户试图访问不存在的资源时,我想重定向到home / index。

是的,关于如何解决这个问题有很多问题。我读过它们,但他们的解决方案对我不起作用。

现在,我在private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { if ( dataGridView1.CurrentCell.ColumnIndex == 5 || dataGridView1.CurrentCell.ColumnIndex == 10)//select target column { TextBox textBox = e.Control as TextBox; if (textBox != null) { textBox.UseSystemPasswordChar = true; } } var txtBox = e.Control as TextBox; txtBox.KeyDown -= new KeyEventHandler(underlyingTextBox_KeyDown); txtBox.KeyDown += new KeyEventHandler(underlyingTextBox_KeyDown); } 上尝试此操作:

web.config

但是,当我尝试访问此网址http://host01/Views/ConnectBatch/Create.cshtml时,我会收到默认的404页面。

问题可能在<system.webServer> <httpErrors> <remove statusCode="404" subStatusCode="-1" /> <error statusCode="404" path="~/Home/Index" responseMode="Redirect" /> </httpErrors> </system.webServer> ,但我尝试使用path="~/Home/Index"并获得了相同的默认404页面。

有什么想法吗?

6 个答案:

答案 0 :(得分:3)

stackoverflow中有很多线程,但是我的情况......正在处理这个

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

原始Thread

  

或者

你应该处理Code背后的错误像这样(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));
        }
    }
}

在您的错误控制器中像这样

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");
    }
}

原始Thread

答案 1 :(得分:2)

这就是我解决问题的方法。也许其他人的答案可以解决它,但我已经测试了其中的一些(我已经对我已经测试过的答案添加了一条评论说,这个答案对我不起作用)。

我已在Web.config <system.web>内添加了此内容:

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

我想在用户找不到资源时重定向到/ Home / Index。

答案 2 :(得分:0)

您可以尝试〜/或〜/ home / index。或者远景将改变views文件夹中的web.config

<强>更新 或者也许this answer是要走的路,当我读到你的问题时,我的第一直觉就是一个处理程序

答案 3 :(得分:0)

你可以尝试使用:

<httpErrors existingResponse="PassThrough" />

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

那是我的帮助:

ASP.NET MVC 404 Error Handling

答案 4 :(得分:0)

尝试使用会话变量... 资源可用时初始化Session变量

例如:

Session["user_name"] = resource.name;

在页面加载功能中检查此变量

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

答案 5 :(得分:-1)

你有什么理由不能使用customErrors吗?

<customErrors mode="On">
    <error statusCode="404" redirect="~/Home/Index" />
</customErrors>
相关问题