I have 3 Class Libraries that my WebApp references. One of these libraries has folders to organize the classes in it. When creating classes in these sub folders I kept all namespaces as default.
I always thought that if you add the namespaces to the web.config you do not have to add them in 'using statements' at the top of individual aspx.cs pages. In a VB.NET Website I think it works but not in a C# WebApp. Is there anything additional that I must do or is it not possible?
<namespaces>
<add namespace="Library1" />
<add namespace="Library2" />
<add namespace="Library3.Admin" />
<add namespace="Library3.CRM" />
</namespaces>
Also I never knew that you couldn't use the App_Code folder in a Web Application until now. Where would you recommend creating a BasePage.cs file.
Related
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" />
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 :(
I'm new to ASP.NET and just start with Beginning ASP.NET 4 in C# and VB. I'm doing exercise on UserControl and I stuck in site-wide registration UserConrols that add registration syntax in web.config like below.
<pages theme="Monochrome">
<controls>
<add tagPrefix="Wrox" tagName="Banner" src="~/Controls/Banner.ascx" />
</controls>
</pages>
After I added this section to web.config everything work fine, since I have two themes in my website so I add new section for other theme.
<pages theme="DarkGrey">
<controls>
<add tagPrefix="Wrox" tagName="Banner" src="~/Controls/Banner.ascx" />
</controls>
</pages>
This time I can't compile my website, my user control doesn't show up at design-time. I want to know why I can't have two <page> section?
The <pages> node refers to all pages in your Website, so you can only have one of it. The theme attribute sets the default theme for all your pages here. You don't have to register the control twice for different themes.
You can set different themes for different areas, but not the way you are trying to do it.
Read How to: Apply ASP.NET Themes:
A theme setting in the Web.config file applies to all ASP.NET Web pages in that application. Theme settings in the Web.config file follow normal configuration hierarchy conventions. For example, to apply a theme to only a subset of pages, you can put the pages in a folder with their own Web.config file or create a element in the root Web.config file to specify a folder. For details, see Configuring Specific Files and Subdirectories.
I'm introducing LESS into an existing ASP.NET web forms application. In order to get intellisense to work, I decided to set up the LessCssHttpHandler to intercept requests for files ending in .less.css. That way, Visual Studio still thinks we're dealing with a CSS file. I did this by adding the following line to my web.config file:
<add type="dotless.Core.LessCssHttpHandler, dotless.Core"
validate="false" path="*.less.css" verb="*" />
In order to get this to work, I had to tweak my IIS settings so that .css files get handled by the ASP.NET framework. Unfortunately, by doing so, now my existing .css files (which aren't handled by the dotless HTTP handler since they don't end in .less.css) aren't returning any content. This makes sense since the ASP.NET framework doesn't really know what to do when it sees a file with that extension.
Is there some sort of base HTTP handler I can set up in addition to the one I have above to handle normal .cssfiles? Something like:
<add verb="*" path="*.css" type="insert some base HTTP handler here that will simply return the contents of the file" />
Looks like the StaticFileHandler is what I was looking for. This is how we ended up adding it to our httpHandlers node in web.config:
<add verb="*" path="*.less.css" validate="false" type="dotless.Core.LessCssHttpHandler, dotless.Core, Version=1.1.0.7, Culture=neutral, PublicKeyToken=96B446C9E63EAE34, processorArchitecture=MSIL" />
<add verb="*" path="*.css" type="System.Web.StaticFileHandler" />
We use Chirpy for our LESS support (as well as our google closure compiler support). It allows you to configure file extensions for LESS, such as .less.css, and then you can have Intellisense support.
It doesn't do translation at runtime but rather at design time within visual studio. When you edit and save the LESS file, Chirpy kicks in and processes the LESS file which generates the css file. This way we avoid having to hand off css file serving to ASP.NET.
I tend to use the console compiler and rename the less file to .css
The httphandler is usually only for people who need parameters in their CSS.
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 :(