English 中文(简体)
Web. config <appSettings>的T4MVC
原标题:T4MVC for Web.config <appSettings>

"""http://t4mvc.codeplex.com/" rel="nofollow" >T4MVC 的好东西是,它允许你摆脱literal/magic tring

T4MVC is a T4 template f或 ASP.NET MVC apps that creates strongly typed helpers that eliminate the use of literal strings when referring the controllers, actions and views.

Web.config 文件内的应用程序设置中,是否可以有类似的设置:

<appSettings>
    <add key="SecurityGuardEmailFrom" value="[email protected]" />
    <add key="Passw或dExpiresInDays" value="1" />
    <add key="NumberOfPassw或dsToKeep" value="5" />
</appSettings>

所以,而不是这个:

private static readonly int Passw或dExpiresInDays =
int.Parse(ConfigurationManager.AppSettings["Passw或dExpiresInDays"]);

我们有这样的东西:

MVC.Webconfig.Passw或dExpiresInDays

MVC.AppSettings.Passw或dExpiresInDays

This would help in compile time to check if the app setting is still there avoiding runtime err或s.

这是否可行?如果可行,你是否知道在某处是否已经实施了类似的做法?

最佳回答

有趣的是, 在我用不同的字眼张贴了这个问题后, 我谷歌上用不同的字眼搜索, 找到了一些东西: T4 模板, 处理内置时有不错的错误... 这里的文章是:

"http://www.wynia.org/wewpress/2010/04/t4-template-for-applates-appings-access-in-config-files" rel=“nofollow”>T4 AppsSettings Access in Config 文件

我不得不更改文章中提供的T, 以便在我当前的环境中运作(VS 11 Beta + ASP.NET MVC 4 App)。

How to use?

下载 < a href=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

这些是Im使用的进口品:

<#@ template language="C#" debug="true" hostspecific="true" #>
<#@ output extension=".cs" #>
<#@ Assembly Name="System.Core.dll" #>
<#@ assembly name="EnvDTE" #>
<#@ Assembly name="System.Configuration"#>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Diagnostics" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.Collections.Specialized"#>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Configuration" #>

Web.config 中的一些应用程序设置在它们的名称中有一个冒号( : ), 例如 < code> webpages: Version 。 这就是我使它发挥作用的手法 :

public static string  <#=setting.Replace(":", "")#>
{
    get
    {
        return getConfigSetting("<#=setting#>");
    }
}

注意 < code> section. replace 上面的 < code> replace 。

如果您愿意的话, 您也可以调试 T4 模板。 请遵循这里描述的步骤 :

< a href=" http://www.hanselman.com/blog/ Tiny HappyFeatures1T4TemplateDebugging InVisualStudio2012.aspx?utm_source=feedburner&utm_medied=feed&utm_campaign=Feed%3a%20ScottHanselman%20%2028Scott%20Hanselman%20-%20ComputerZen.com%29&utm_content=Google%20Reader” rel=“nofolve” 提示#1 -2012视觉工作室T4模板调试

问题回答

创建自定义配置区域, 而不是在 中设置设置 。

http://msdn.microsoft.com/en-us/library/2tw134k3.aspx" rel=“没有跟随 noreferrer">http://msdn.microsoft.com/en-us/library/2tw134k3.aspx

You can extend ASP.NET configuration settings with XML configuration elements of your own. To do this, you create a custom configuration section handler. The handler must be a .NET Framework class that inherits from the System.Configuration.ConfigurationSection class. The section handler interprets and processes the settings that are defined in XML configuration elements in a specific section of a Web.config file. You can read and write these settings through the handler s properties.





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签