Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在MVC中将错误重定向到主页?_C#_Model View Controller - Fatal编程技术网

C# 如何在MVC中将错误重定向到主页?

C# 如何在MVC中将错误重定向到主页?,c#,model-view-controller,C#,Model View Controller,当我在根页面上时,我试图让我的网站自动默认回到主页。如果我输入了一个错误的URL,我该如何在MVC中做到这一点 我只是在学习MVC,还没弄明白。。。我有这个: namespace DvdApplication { public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("

当我在根页面上时,我试图让我的网站自动默认回到主页。如果我输入了一个错误的URL,我该如何在MVC中做到这一点

我只是在学习MVC,还没弄明白。。。我有这个:

namespace DvdApplication
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Dvd", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}

您可以使用web.config或HandleError属性轻松实现这一点:

<system.webServer>
    <httpErrors errorMode="Custom" existingResponse="Replace">
      <remove statusCode="401" subStatusCode="-1" />
      <error statusCode="401" path="/Error/Forbidden" responseMode="ExecuteURL" />
      <remove statusCode="404" subStatusCode="-1" />
      <error statusCode="404" path="/Error/NotFound" responseMode="ExecuteURL" />
      <remove statusCode="500" subStatusCode="-1" />
      <error statusCode="500" path="/Error/Generic" responseMode="ExecuteURL" />
      <remove statusCode="501" subStatusCode="-1" />
      <error statusCode="501" path="/Error/Generic" responseMode="ExecuteURL" />
    </httpErrors>
</system.webServer>

注:注意,应存在控制器“错误”和操作“禁止”、“未找到”和“通用”

[HandleError(ExceptionType=typeof(SqlException),View=“DatabaseError”)]] [HandleError(ExceptionType=typeof(NullReferenceException),View=“LameErrorHandling”)]]

更多信息: