How to compile a C# file outside of App_Code? - asp.net

I'd like to stick a class down in my folder hierarchy. The scenario is too trivial to warrant it's own project or separate website. However, I hate to clutter my top-level App_Code with something that's used by a tiny corner of the site.
Is there a way in web.config to include another file or folder in the compilation process?

It sounds like you are using the "Web Site" project type. You might consider switching to the "Web Application" project type which works more like a traditional project, allowing you to have a much more flexible folder structure (code can go anywhere you like, and App_Code isn't a special folder).
This post has a brief discussion and links on the pros/cons of Web Site vs. Web Application projects:
http://forums.asp.net/p/1233004/2232697.aspx#2232697

<configuration>
<system.web>
<compilation>
<assemblies>
<add assembly="<AssemblyName>, Version=<Version>, Culture=<Culture>, PublicKeyToken=<PublicKeyToken>"/>
</assemblies>
</compilation>
</system.web>
</configuration>

Related

Web.config add namespace not applied at file level

I am referencing a class library in a web application project (both in same solution). Within the web pages of the web application, if I do this:
If MyValidation.CorrectEmailFormat(email) Then ...
...Visual Studio 2013 underlines the method, and suggests I import MyCompany.EmailMethods at the top of the page. If I do the Import, the page compiles and the method works okay.
However, because these methods are used extensively across the application, I don't want to add them at page level every time. So I headed for web.config, and did this:
<pages>
<namespaces>
<add namespace="MyCompany.EmailMethods" />
</namespaces>
</pages>
However, VS is still prompting me to perform the Import at the top of every page, and the method is not recognised in the page without doing this. What I am doing wrong please? I assumed from MSDN and other sources this was the correct way to achieve this.
Web application is ASP.Net web pages (4.6).
The reference must be added to the Imported Namespace as described in the following SO post
add-a-namespace-reference-to-all-web-application-web-pages-in-a-different-project
It must be added in the project properties page at the bottom part titled Imported Namespaces
The <pages> directive applies to ASPX files only.
You need to use the equivalent directive for Razor:
<system.web.webPages.razor>
<pages>
<namespaces>
<add namespace="MyCompany.EmailMethods" />

Is there a "release" configuration for ASP.NET Web Sites?

I use Visual Studio for Website development (VS 2010 Ultimate and VS 2012 Professional). To be more specific, I created this website by File > New Web Site, so I do not believe this is a project.
While developing the website, I have debug="true" enabled in the web.config file. When I publish, I manually change to debug="false".
<configuration>
<system.web>
<compilation debug="true" strict="true" explicit="true" targetFramework="4.5">
</system.web>
</configuration>
There are two config files in the solution: "web.config" and "web.Debug.config".
This is what the various forum articles and "Programming ASP.NET" books say to do, but I wonder if there is a way to have debug="true" for local development and automatically switch to debug="false" when using Build > Publish Web Site so I don't have to manually change the web.config file?
According to this forum answer, "There is no way to have a Release configuration for your website."
Keeping in mind that this is a Web Site and not a project, it looks like adding another config based on comment suggestions might not be possible. A possibly valid answer is "no, it is not possible in this context."
Is there another way to achieve the intended outcome without using the current workaround of manually changing the debug setting?
Sorry, it's related to it being a "web site" type project, which aren't compiled:
Configuration of publishing an ASP.NET web site
To quote the previous responder above:
"Web Site projects don't have the Release configuration available, but it makes no difference since they are not compiled. Web Application projects, on the other hand, do get compiled and have both configurations available."
According to the available references, for "ASP.NET Website" it is not possible to have a separate release configuration.
So the answer to the posed question is no: it is not currently possible. Manually changing the debug attribute when you publish and then changing it back is the only option in that case.
How can you proceed? If you really need to have a release configuration and a debug configuration, the a possible option is Converting a Web Site Project to a Web Application Project. While not a direct answer to the presented question, it is an alternative.
For some projects I've set up an Environment appSetting and scoped all other keys off of that Environment.
For instance:
<add key="Environment" value="Development"/>
<add key="Development.Title" value="My Dev App"/>
<add key="Production.Title" value="My Production App"/>
<connectionStrings>
<add name="DbContext.Development" connectionString="Initial Catalog=DatabaseDev;...."
providerName="System.Data.SqlClient" />
<add name="DbContext.Production" connectionString="Initial Catalog=DatabaseProd;...."
providerName="System.Data.SqlClient" />
</connectionStrings>
Then you would create a Configuration class that would pull appSettings and connectionStrings by looking for:
appSetting
string.Format("{0}.Title", ConfigurationManager.AppSettings["Environment"])
connectionString
string.Format("DbContext.{0}", ConfigurationManager.AppSettings["Environment"])
Not perfect but this will let you only have to replace one web.config value instead of a bunch without the help of the Publish config transform.

How to add another file extension to Razor ViewEngine, e.g. *.html [duplicate]

I manage a large asp.net site which has previously been converted from static html site to asp.net.
For several reasons (mainly SEO) we decided not to rename all the files to .aspx back when we originally converted the site. This was very easy to do by simply adding the buildProvider and httpHandler to the web.config.
<buildProviders>
<add extension=".html" type="System.Web.Compilation.PageBuildProvider"/>
</buildProviders>
<httpHandlers>
<add path="*.html" verb="*" type="System.Web.UI.PageHandlerFactory"/>
</httpHandlers>
Now I am upgrading the site to use Asp.net WebPages with Razor cshtml files. I can rename all the files if necessary, and use url rewriting to make the urls stay the same, however it would be much easier if I could just configure the web.config to tell it to parse .html files as if they were .cshtml.
I have searched around quite a bit, and could not find anything equivalent to the PageHandlerFactory for razor pages. It appears as though it is just an internal mechanism in the .net 4.0 ISAPI handler.
The site is currently running on Windows 2003 server and IIS 6. We will be upgrading to 2008/IIS 7.5 in the near future, but I'd prefer not to wait for that.
Is there any way to get the .html files to be parsed by razor as if they were .cshtml files?
Thank you to SLaks for pointing me in the right direction, but it still took a few hours of digging in the MVC source to figure out the solution.
1 - Need to put RazorBuildProvider in web.config
<buildProviders>
<add extension=".html" type="System.Web.WebPages.Razor.RazorBuildProvider"/>
</buildProviders>
And add System.Web.WebPages.Razor to assemblies if it isn't already there.
<assemblies>
[...]
<add assembly="System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
2 - Add 2 lines in global.asax Application_Start() method
// Requires reference to System.Web.WebPages.Razor
System.Web.Razor.RazorCodeLanguage.Languages.Add(
"html", new CSharpRazorCodeLanguage());
WebPageHttpHandler.RegisterExtension("html");
Call WebPageHttpHandler.RegisterExtension.
You may also need to register a custom WebPageRazorHostFactory to tell the Razor engine what to do with the file; I'm not sure.
As this actually been resolved for use with VS2012 / .net 4.5.
As using the examples above in a C#5 project I get no luck :(

Can I serve .html files using Razor as if they were .cshtml files without changing the extension of all my pages?

I manage a large asp.net site which has previously been converted from static html site to asp.net.
For several reasons (mainly SEO) we decided not to rename all the files to .aspx back when we originally converted the site. This was very easy to do by simply adding the buildProvider and httpHandler to the web.config.
<buildProviders>
<add extension=".html" type="System.Web.Compilation.PageBuildProvider"/>
</buildProviders>
<httpHandlers>
<add path="*.html" verb="*" type="System.Web.UI.PageHandlerFactory"/>
</httpHandlers>
Now I am upgrading the site to use Asp.net WebPages with Razor cshtml files. I can rename all the files if necessary, and use url rewriting to make the urls stay the same, however it would be much easier if I could just configure the web.config to tell it to parse .html files as if they were .cshtml.
I have searched around quite a bit, and could not find anything equivalent to the PageHandlerFactory for razor pages. It appears as though it is just an internal mechanism in the .net 4.0 ISAPI handler.
The site is currently running on Windows 2003 server and IIS 6. We will be upgrading to 2008/IIS 7.5 in the near future, but I'd prefer not to wait for that.
Is there any way to get the .html files to be parsed by razor as if they were .cshtml files?
Thank you to SLaks for pointing me in the right direction, but it still took a few hours of digging in the MVC source to figure out the solution.
1 - Need to put RazorBuildProvider in web.config
<buildProviders>
<add extension=".html" type="System.Web.WebPages.Razor.RazorBuildProvider"/>
</buildProviders>
And add System.Web.WebPages.Razor to assemblies if it isn't already there.
<assemblies>
[...]
<add assembly="System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
2 - Add 2 lines in global.asax Application_Start() method
// Requires reference to System.Web.WebPages.Razor
System.Web.Razor.RazorCodeLanguage.Languages.Add(
"html", new CSharpRazorCodeLanguage());
WebPageHttpHandler.RegisterExtension("html");
Call WebPageHttpHandler.RegisterExtension.
You may also need to register a custom WebPageRazorHostFactory to tell the Razor engine what to do with the file; I'm not sure.
As this actually been resolved for use with VS2012 / .net 4.5.
As using the examples above in a C#5 project I get no luck :(

Referencing code in App_Code from web.config

I have a type in my App_Code folder from a Web Site project that I want to refer to in Web.config. The type attribute is requiring me to put in an assembly name. The internets is failing me with what to put in for the assembly.
Specifically in,
<system.web>
<webServices>
<soapExtensionReflectorTypes>
<add type="MyType, $App_Code$" />
</soapExtensionReflectorTypes>
</webServices>
</system.web>
What do I put in $App_Code$ to make it compile? I've tried _ _ code, App _code, App _Code (Markdown is failing here: those type names don't have spaces in them)
OK, I found the answer on some obscure MSDN forum: you can't do that in a Web Site project for system.web/webservices/soapExtensionReflectorTypes. Only a Web Application Project will suffice.
I found a work around for a Web Site project. I moved the type out of the App_Code folder and put it in a new class library, compiled and added to the bin directory.
Now the web.config section finds the type through
<soapExtensionReflectorTypes>
<add type="MyNamespace.MyType, FileTitleOfLibrary"/>
</soapExtensionReflectorTypes>
The second argument is the filename of the library without the file extension.

Resources