I tend to make a lot of folders开发者_如何学编程 and when doing OO I put the Models in a folder called Models then under that I have BLL, DAL, OBJ
Instead of writing at the top of all of my files using MyApp.Models;
I would like to put this in the Web.Config if possible.
I am new to .net so I am not sure if you can or even if this is a good idea.
I don't believe there is any way to do that in the config file for C# files.
This isn't exactly what you're asking for, but the closest thing to it is that you can register namespaces in the web.config that are then usable within ascx/aspx markup files, as Steven mentioned. Below is an example:
<configuration>
<system.web>
<pages>
<namespaces>
<add namespace="System.Web.Mvc"/>
<add namespace="System.Web.Mvc.Ajax"/>
<add namespace="System.Web.Mvc.Html"/>
<add namespace="System.Web.Routing"/>
<add namespace="System.Linq"/>
<add namespace="System.Collections.Generic"/>
<!-- Add your own namespaces here -->
</namespaces>
</pages>
</system.web>
</configuration>
EDIT: added another suggestion below:
If you had several using statements that had to be included within most of your .cs files, then the best thing I could suggest is to either A) keep those using statements handy where you can just copy/paste them as needed, or B) create a C# code snippet, such as the example below:
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>using MyApp</Title>
<Shortcut>usingmyapp</Shortcut>
<Description>Code snippet for common using statements with MyApp</Description>
<Author></Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Code Language="csharp">
<![CDATA[using MyApp.Common;
using MyApp.Common.Extensions;
using MyApp.Domain;
using MyApp.Models;
// *** add whatever using statements you want here ***
$end$]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
After you add that file into the appropriate location (...\My Documents\Visual Studio 2008\Code Snippets\Visual C#\My Code Snippets), then you should then just be able to start typing "usingmyapp" (or whatever you put inside the <Shortcut>
element) at the location of your .cs file where you want the using statements to appear; intellisense should recognize it and autocomplete it for you by inserting the <Code>
content of the code snippet.
Personally, I don't bother with doing something like this; I don't like to add a bunch of using statements to my file if some of them are not actually being used in that file. Additionally, I think adding several using statements in bulk could potentially lead to confusing ambiguous name conflicts if you start using a class name that appears in multiple namespaces. But I think that's just a personal preference; if you want to do this, go ahead.
It is possible to register assemblies and namespaces for use in your markup, but not for use in your code behind of code files. VB.NET does support this on a project level, but I never liked this feature, because it makes it less explicit what you're using.
It is not possible.
Class names have to be resolved at compile time and web.config can change at any time after deployment.
精彩评论