6

我只想做以下事情:

var defaultRedirectUrl = SomeMethodToGetDefaultRedirect();

当然在 web.config 我有

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

这该怎么做?

4

2 回答 2

13

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

根据您的帖子,答案是:

    CustomErrorsSection customErrorsSection = (CustomErrorsSection) ConfigurationManager.GetSection("system.web/customErrors");
        string defaultRedirect = customErrorsSection.DefaultRedirect;
于 2012-05-09T07:36:27.653 回答
12

如果我正确理解您的问题,这应该会有所帮助(从 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;
于 2012-05-08T23:50:54.303 回答