I am using VS 2008 and C# but when I added namespace in web.config file, that namespace is not imported or included in code behind or aspx
I have also read this question but not get the required answer.
web.config code
<configuration>
<system.web>
<pages>
<namespaces>
<add namespace="System.Data" />
<add namespace="System.Text"/>
</namespaces>
</pages>
</system.web>
</configuration>
You need to put them in the correct <system.web> section. e.g.:
<configuration>
<system.web>
<pages>
<namespaces>
<add namespace="System.Data" />
<add namespace="System.Text"/>
</namespaces>
</pages>
</system.web>
</configuration>
and put them in the correct web.config
i.e. the second web.config file is the Views folder and is specific to views. These settings do not go in the root web.config.
The purpose of these settings is to make the libraries available to the ASPX pages (e.g. for Intellisense) and it is not used for the code-behind. You still need to have using statements in your actual code as that is just plain c# programming.
The purpose of the namespace section is to get around having to do the import in the .aspx page. Code behind in C# still requires you to have the using statements at the top of your .cs file.
There is no way to get around this.
Related
I have made an Asp.Net webforms website in C# using the .net framework 4.6.1.
The website works perfectly but when I try to optimize the CSS load using the weboptimization component I get a 404 from the css url request.
Please note that I have also the js bundle that works perfectly.
in the .aspx page I have in the head:
<webopt:BundleReference runat="server" Path="~/stylesheets" />
the bundle.config is in the root of the site structure and its content is:
<?xml version="1.0" encoding="utf-8" ?>
<bundles version="1.2">
<styleBundle path="~/stylesheets">
<include path="~/Bootstrap/css/bootstrap.css" />
<include path="~/css/style.css" />
<include path="~/css/custom.css" />
</styleBundle>
</bundles>
Inspecting the network request I see that the request for stylesheets
where base-site is instead the js bundle
Global.asax file:
void Application_Start(object sender, EventArgs e)
{
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleTable.EnableOptimizations = true;
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
web.config:
<system.web>
<authentication mode="None"/>
<compilation debug="true" targetFramework="4.6.1">
...
<httpRuntime targetFramework="4.6.1"/>
<pages enableEventValidation="true">
<namespaces>
<add namespace="System.Web.Optimization"/>
<add namespace="Microsoft.AspNet.Identity"/>
</namespaces>
<controls>
<add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt"/>
</controls>
</pages>
...
</system.web>
<system.webServer>
<modules>
<remove name="FormsAuthentication"/>
<remove name="BundleModule"/>
<add name="BundleModule" type="System.Web.Optimization.BundleModule"/>
</modules>
...
</system.webServer>
I have an identical website with the same web.config, same routing configuration with EnableFriendlyUrls(True) where the CSS bundle is correctly provided.
It doesn't work both local on VS2017 and on the production server.
There is no folder or other pages named "stylesheets"
The CSS files are well-formed
I've tried also with a single CSS file, but I get always a 404.
The routing for all the other pages and JS bundle works fine
Why the CSS bundle doesn't work?
Is there something I can do to understand where is the issue?
After several tests, I have found that every time I make a change in the css bundle.config file I have to close and restart Visual Studio.
Restarting IIS Express is not enough.
On the production server, I have to stop and start again the website, recycling the application pool.
Please am fairly new to programming in asp.net and now am following the steps in a textbook to design an e-commerce web application.
I am using UrlRewritingNet dll from http://www.urlrewriting.net/ to create search engine friendly urls on the application. I unzipped the urlrewritingnet zip and copied its dll file to my application's bin folder. I also copied its urlrewritingnet.xsd file to my project folder. The textbook am working from uses the dll from urlrewriter.net with different configurations which I tried in my project but it didn't work. The textbook also uses visual web developer 2005. Am programming in visual studio 2012 (C#). For now am trying to rewrite the url of my catalogs page so as to test if the configuration in my web.config file is correct. I'm doing the configuration in my web.config, not in IIS. I do not want any IIS configurations because am imagining a scenario where I don't have access to the server.
I keep getting an internal server error that displays like this:
HTTP Error 500.22 - Internal Server Error
An ASP.NET setting has been detected that does not apply in Integrated managed pipeline mode.
this is the url which I want to rewrite: http://localhost:1036/Catalog.aspx?DepartmentID=1
This is my config file:
<configuration>
<configSections>
<section name="urlrewritingnet"
restartOnExternalChanges="true"
requirePermission ="false"
type="UrlRewritingNet.Configuration.UrlRewriteSection, UrlRewritingNet.UrlRewriter" />
</configSections>
<urlrewritingnet
rewriteOnlyVirtualUrls="true"
contextItemsPrefix="QueryString"
defaultProvider = "RegEx"
defaultPage = "Default.aspx"
xmlns="http://www.urlrewriting.net/schemas/config/2006/07" >
<rewrites>
<add name="Rule1"
provider="RegEx"
virtualUrl="^.*-d([0-9]+)/?$"
rewriteUrlParameter="ExcludeFromClientQueryString"
destinationUrl="~/Catalog.aspx?DepartmentID=$1"
ignoreCase="true" />
</rewrites>
</urlrewritingnet>
<appSettings>
<add key="MailServer" value="smtp.gmail.com"/>
<add key="MailUsername" value="engr.ejikeeze#gmail.com"/>
<add key="MailPassword" value="keeplooking"/>
<add key="MailFrom" value="engr.ejikeeze#gmail.com"/>
<add key="EnableErrorLogEmail" value="false"/>
<add key="ErrorLogEmail" value="engr.ejikeeze#gmail.com"/>
<add key="ProductsPerPage" value="6"/>
<add key="ProductDescriptionLength" value="60"/>
<add key="SiteName" value="BalloonShop"/>
</appSettings>
<connectionStrings>
<add name="BalloonShopConnection" connectionString="server = (local)\SQLEXPRESS; Database = BalloonShop; User = balloonshop; password=ecommerce" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<httpModules>
<add name="UrlRewriteModule"
type="UrlRewritingNet.Web.UrlRewriteModule, UrlRewritingNet.UrlRewriter" />
</httpModules>
<globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="en-US"/>
<customErrors mode="RemoteOnly" defaultRedirect="Oops.aspx" />
<pages theme="BalloonShopDefault">
<controls>
<add src="~/UserControls/DepartmentsList.ascx" tagName="DepartmentsList" tagPrefix="BSUC"/>
</controls>
</pages>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
</configuration>
Pls I need you lovely guys to help me out. Am really stuck. I've searched all round the internet for over 4 days in search of solution but to no avail. Thanks in anticipation.
You can checkout the following link:url-rewriting-in-Asp-Net
I'm creating a custom server control which uploads files asynchronously to the server.
This solution uses flash element that posts the files to Generic Web handler aka ashx which then saves the posted file in a desired location.
It works grate, but with this approach I do need to create ashx file in each project, potentially, to handle the posts from the flash element.
What I would like to achieve is fully encapsulated server control that will use it's own ashx (or whatever can replace it) handler to upload the files.
Is it possible to do? Any ideas would be welcome.
Thanks.
You could include the handler within the class library containing the control and then only register it in web.config using the <httpHandlers> section:
<configuration>
<system.web>
<httpHandlers>
<add verb="*"
path="upload.ashx"
type="MyControl.MyUploadHandler.New, MyControl" />
</httpHandlers>
<system.web>
</configuration>
and if you are using IIS 7 Integrated pipeline mode to the <handlers> section:
<system.webServer>
<handlers>
<add name="UploadHandler"
verb="*"
path="upload.ashx"
type="MyControl.MyUploadHandler.New, MyControl" />
</handlers>
</system.webServer>
And if you are using ASP.NET 4.0 you could checkout the PreApplicationStartMethod infrastructure which allows you to dynamically register handlers:
[assembly: WebActivator.PreApplicationStartMethod(typeof(MyControl.StartUp), "PreApplicationStart")]
What is the advantage of adding namespace in web.config file?
<configuration>
<system.web>
<pages>
<namespaces>
<add namespace="System.Collections" />
</namespaces>
</pages>
</system.web>
</configuration>
This will allow to add the namespaces of the items you commonly use in your views so you don't need to add the references in each page / view when you need to use them.
Namespaces you define here are automatically imported into all pages of an application.
We want to use Html.RenderAction in our MVC1.0 project. I have downloaded the Futures Microsoft.Web.Mvc dll from codeplex, copied it to the bin folder in our project, and added a reference to it.
However, when I try to use it in one of our views, Html.RenderAction is still not available.
It might be just a silly detail I'm missing, but I have no idea what else I have to do :(
P.S. I checked, and I can actually write "Microsoft.Web.Mvc.ViewExtensions.RenderAction" and Visual Studio autocompletes the code just fine, so it seems the dll is correctly included in the project.
You have to add it to the "namespaces" tag in web.config.
<system.web>
<pages>
<namespaces>
<add namespace="Microsoft.Web.Mvc"/>
</namespaces>
</pages>
</system.web>
RenderAction is part of the MvcFutures project.
Kindness,
Dan
You might need to modify your web.config:
<compilation debug="true">
<assemblies>
<add assembly="Microsoft.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</assemblies>
</compilation>
<pages>
<namespaces>
<add namespace="Microsoft.Web.Mvc"/>
</namespaces>
</pages>