更改自定义错误模式在Web配置编程在ASP.NET MVC 3(Change Custom Erro

2019-07-30 02:51发布

我怎样才能改变编程web.config文件中定义错误模式? 我需要改变如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<configSections />

...

<system.web>
....

<customErrors mode="RemoteOnly">
   <error statusCode="404" redirect="~/errors/404" />
</customErrors>

<customErrors mode="off">
   <error statusCode="404" redirect="~/errors/404" />
</customErrors>

没有任何一个有关于这个任何想法?

UPDATE

由于代码冰火回答,我尝试使用这个:

Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
CustomErrorsSection CustomErrorsection = (CustomErrorsSection)config.GetSection("system.web/customErrors");
CustomErrorsection.Mode = CustomErrorsMode.Off;
config.Save();

但有一个例外:

Access to the path 'D:\Projects\MyProject\web.config' is denied.

Answer 1:

我发现了一个例子几天前这在自定义错误CONFIGRATION很多改变

public static void ConfigureCustomErrors()
    {
        Configuration config = 
        WebConfigurationManager.OpenWebConfiguration("~");

        CustomErrorsSection section = 
            (CustomErrorsSection)config.GetSection(
                "system.web/customErrors");

        //Verify that customErrors exists in web.config
        if (section != null)
        {
            //Only configure if customErrors is enabled 
            if (section.Mode != CustomErrorsMode.Off)
            {
                if(!section.IsReadOnly() && 
                    !section.SectionInformation.IsLocked)
                {
                    //Collection of new redirects to add to 
                    //the customErrors element
                    CustomErrorCollection redirectsToAdd = 
                       new CustomErrorCollection();

                    //Page ID of the page to be used for 
                    //custom error redirects
                    int redirectPageId = 0;

                    //Get existing redirects, if any
                    CustomError redirect404 = 
                        section.Errors["404"];
                    CustomError redirect500 = 
                        section.Errors["500"];

                    //Get URL for 404 redirects
                    int.TryParse(
                        ConfigurationManager.AppSettings[
                             "FileNotFoundPageId"], 
                             out redirectPageId);
                    string fileNotFoundURL = 
                        ToolBox.GetSimpleAddress(
                        DataFactory.Instance.GetPage(
                        new PageReference(redirectPageId));

                    //Get URL for server error redirects
                    int.TryParse(
                        ConfigurationManager.AppSettings[
                        "GenericErrorPageId"], 
                        out redirectPageId);
                    string serverErrorURL = 
                        ToolBox.GetSimpleAddress(
                        DataFactory.Instance.GetPage(
                        new PageReference(redirectPageId)));

                    //If the 404 redirect hasn't been 
                    //specified or if its redirect 
                    //URL is invalid
                    if (fileNotFoundURL!=string.Empty && 
                       (redirect404 == null || 
                       redirect404.Redirect!=
                          fileNotFoundURL))
                    {
                        //Add new 
                        if (redirect404 == null)
                        {
                            CustomError fileNotFoundError = 
                            new CustomError(404,
                            fileNotFoundURL);

                            redirectsToAdd.Add(
                               fileNotFoundError);
                        }
                        else //Modify existing
                        {
                            redirect404.Redirect = 
                                fileNotFoundURL;
                        }
                    }

                    //If the 500 redirect hasn't been 
                    //specified or if its redirect 
                    //URL is invalid
                    if (fileNotFoundURL != string.Empty && 
                        (redirect500 == null || 
                        redirect500.Redirect != 
                           fileNotFoundURL))
                    {
                        //Add new 
                        if (redirect500 == null)
                        {
                            CustomError serverError = 
                            new CustomError(500, 
                            serverErrorURL);

                            redirectsToAdd.Add(serverError);
                        }
                        else //Modify existing redirect
                        {
                            redirect500.Redirect = 
                                serverErrorURL;
                        }
                    }

                    //Add any new redirects
                    foreach (
                        CustomError redirectToAdd in 
                        redirectsToAdd)
                    {
                        section.Errors.Add(redirectToAdd);
                    }

                    //Save web.config if its 
                    //contents have changed
                    config.Save();
                }
            }
        }
    }


Answer 2:

为了更新你的web.config文件,运行你的应用程序池的过程中/身份需要有修改(写)访问web.config文件。 你得到一个错误,因为你的应用程序没有权限写入到web.config文件。 添加适当的权限,哪些已经指出无一例外会工作。 现在,随着中说...

免责声明

配置文件驱动应用程序,具有对您的应用程序,计算机密钥(也许)用于编码/解码身份验证票证,SQL连接字符串,等等。有了这个可写路径认证/授权信息,你的应用程序可能是一个讨厌的安全问题。 我已经作出了声明。 请小心 :)



文章来源: Change Custom Error Mode in Web Config Programmatically in ASP.NET MVC 3