如何从代码中获取customErrors的defaultRedirect值?

时间:2022-08-25 21:49:09

I would simply like to do something as follows:

我只想做以下事情:

var defaultRedirectUrl = SomeMethodToGetDefaultRedirect();

of course in web.config I have

当然在web.config我有

<customErrors mode="On" defaultRedirect="~/Error"/>

How to do this?

这个怎么做?

2 个解决方案

#1


12  

Thanx mark, it was helpful. What I really wanted to ask was "how to get "defaultRedirect" property of customErrors section from web.config of my asp mvc app?".

Thanx标记,很有帮助。我真正想问的是“如何从我的asp mvc应用程序的web.config中获取customErrors部分的defaultRedirect属性?”。

And the answer, based on your post is:

答案,基于你的帖子是:

    CustomErrorsSection customErrorsSection = (CustomErrorsSection) ConfigurationManager.GetSection("system.web/customErrors");
        string defaultRedirect = customErrorsSection.DefaultRedirect;

#2


12  

If I understand your question correctly, this should help (copied from msdn)

如果我理解你的问题,这应该有帮助(从msdn复制)

// Get the Web application configuration.
Configuration configuration =  WebConfigurationManager.OpenWebConfiguration( "/aspnetTest");

// Get the section.
CustomErrorsSection customErrorsSection = (CustomErrorsSection)configuration.GetSection("system.web/customErrors");

// Get the collection
CustomErrorCollection customErrorsCollection =  customErrorsSection.Errors;

// Get the currentDefaultRedirect
string currentDefaultRedirect =  customErrorsSection.DefaultRedirect;

#1


12  

Thanx mark, it was helpful. What I really wanted to ask was "how to get "defaultRedirect" property of customErrors section from web.config of my asp mvc app?".

Thanx标记,很有帮助。我真正想问的是“如何从我的asp mvc应用程序的web.config中获取customErrors部分的defaultRedirect属性?”。

And the answer, based on your post is:

答案,基于你的帖子是:

    CustomErrorsSection customErrorsSection = (CustomErrorsSection) ConfigurationManager.GetSection("system.web/customErrors");
        string defaultRedirect = customErrorsSection.DefaultRedirect;

#2


12  

If I understand your question correctly, this should help (copied from msdn)

如果我理解你的问题,这应该有帮助(从msdn复制)

// Get the Web application configuration.
Configuration configuration =  WebConfigurationManager.OpenWebConfiguration( "/aspnetTest");

// Get the section.
CustomErrorsSection customErrorsSection = (CustomErrorsSection)configuration.GetSection("system.web/customErrors");

// Get the collection
CustomErrorCollection customErrorsCollection =  customErrorsSection.Errors;

// Get the currentDefaultRedirect
string currentDefaultRedirect =  customErrorsSection.DefaultRedirect;