Showing posts with label Asp.net Configuration. Show all posts
Showing posts with label Asp.net Configuration. Show all posts

Thursday, August 12, 2010

How to change asp.net control attribute rendering and encording

Sometimes you may find when you add attributes to web controls like
this.txtName.Attributes.Add("onkeryup""javascript:CheckIt('email')");
.net renders the controls like
<input name="txtName" type="text" id="txtName" onkeryup="javascript:CheckIt(&#39;email&#39;)" />
As well sometimes you may get problems with do postback with options as asp.net renders
<input 
    type="submit" 
    name="btnSave" 
    value="Save" 
    onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(
        &quot;btnSave&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" 
    id="Submit1" />
In such cases, if this is not the case you are looking for, you may find it is useful to plug in custom attribute encorder and take control of how you want to render your attributes. This example below demonstrates how to plug in a custom url encorder and implment simple attribute encorder.
Web.config
<httpRuntime encoderType="ActiveTest.HtmlAttributeEncoder" ... />
Attribute encorder class
namespace ActiveTest
{
    public class HtmlAttributeEncoder : HttpEncoder
    {
        protected override void HtmlAttributeEncode(string value, System.IO.TextWriter output)
        {
            value = value.Replace("\"""'");
            output.Write(value);
        }
    }
}

Wednesday, August 11, 2010

How to occupy multiple site map files in a single website in asp.net

There are some occations where we need to provide two menu structures. For example top navigation and left navigation. In such cases we can easily configure two XML Sitemap providers in web.config and use them separately in the asp.net pages
Web.config
<siteMap>
  <providers>
    <add siteMapFile="topNavigation.sitemap" name="TopNavigationSiteMapProvider"
        type="System.Web.XmlSiteMapProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    <add siteMapFile="leftNavigation.sitemap" name="leftNavigationSiteMapProvider"
        type="System.Web.XmlSiteMapProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
  </providers>
</siteMap>

Markup
<asp:SiteMapDataSource ID="smdTopNavigation" SiteMapProvider="TopNavigationSiteMapProvider" runat="server" />
<asp:Menu runat="server" ID="mnuTopNavigation" DataSourceID="smdTopNavigation" />
 
<asp:SiteMapDataSource ID="smdLeftNavigation" SiteMapProvider="LeftNavigationSiteMapProvider" runat="server" />
<asp:Menu runat="server" ID="menuLeftNavigation" DataSourceID="smdLeftNavigation" />

How to set dynamic connection string to SQLDataSource

There are some occations where we cant have fixed connection string in the web.config but we have to utilize username and password from the current context to login to database server. So such a case having SqlDataSource declared declaratively in <%$ ... %>,  best option to attach an on-init event handler to SqlDataSource.
WebConfig.
<connectionStrings>
  <clear/>
  <add name="LocalTestSqlServer" 
        connectionString="Data Source=CHARITH; Initial Catalog=Test; User ID={0}; Password={1}"/>
</connectionStrings>

Consuming Page:
<%@ Page Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
 <script runat="server">
        protected void SetCredentials(object sender, EventArgs e)
        {
            SqlDataSource sdt = sender as SqlDataSource;
            sdt.ConnectionString = string.Format(sdt.ConnectionString, "UserName""Password");
        }
 </script>
</head>
<body>
 <form id="form1" runat="server">                
        <asp:SqlDataSource 
            ConnectionString="<%$ ConnectionStrings:LocalSqlServer %>" 
            ID="SqlDataSource2" 
            OnInit="SetCredentials"
            runat="server" />
 </form>
</body>
</html>

Monday, July 26, 2010

How to handle large file uploads which exceeds the configured request length

Say you have in web config :
Snippet
<httpRuntime maxRequestLength="4048" />

Cause:
When you try to upload a file grater then 4MB you will get an error with the status of 404.13. To catch this error we have to utilise Application_Error as .net framework does not generate a request object and initiate a page lifecycle for this type of request. So no events get fired, but first of all you will end up in a error page.
What we can do is:
In Global.asax file:
protected void Application_Error(object sender, EventArgs e)
{
    if (HttpContext.Current.Request.Url.ToString().Contains("FileUpload.aspx") && 
        HttpContext.Current.Error.InnerException.Message.Contains("Maximum request length exceeded"))
    {
        HttpContext.Current.ClearError();
        HttpContext.Current.Response.Redirect(
            string.Format("~/FileUpload.aspx?Message={0}""Upload file is over the allowed limit"));
    }
}

Then you can FileUpload.aspx like this:
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <script runat="server">
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            string message = this.Request.QueryString.Get("Message");
            if (!string.IsNullOrEmpty(message)) this.lblMessage.Text = message;
            else this.lblMessage.Visible = false;
        }
        protected void Save(object sender, EventArgs e)
        {
            
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label runat="server" ID="lblMessage" />
        <asp:FileUpload runat="server" ID="fuFile" />
        <asp:Button runat="server" ID="btnSave" Text="Save" />
    </div>
    </form>
</body>
</html>

Monday, May 31, 2010

ASP.net and ATL Server debugging - IIS 7

Cause:
Error while trying to run project: Unable to start debugging on the web server. The server does not support debugging of ASP.NET or ATL Server applications. Run setup to install the Visual Studio .NET server components. If setup has been run, verify that a valid URL has been specified.


You may also want to refer to the ASP.NET and ATL Server debugging topic in the online documentation. Would you like to disable future attempts to debug ASP.NET pages for this project?
Posible reson is to have in-compatile web.config after converting asp.net 3.5 application to asp.net 4.0 application.

Example: having modules and httpModules in the same web.config

<configuration>
  ...
  <system.web>
    ...
    <httpModules>
      <add name="SEOModule"
         type="WebSphere.Ngate.SEOModule.SEOModule, WebSphere.Ngate.SEOModule" />
    </httpModules>
  </system.web>
  <system.webServer>
    <modules>
      <add name="SEOModule" preCondition="managedHandler"
         type="WebSphere.Ngate.SEOModule.SEOModule, WebSphere.Ngate.SEOModule" />
    </modules>
    ...
  </system.webServer>
</configuration>

Removing httpModules section will resolve the problem

<configuration>
  ...
  <system.web>
    ...
  </system.web>
  <system.webServer>
    <modules>
      <add name="SEOModule" preCondition="managedHandler"
         type="WebSphere.Ngate.SEOModule.SEOModule, WebSphere.Ngate.SEOModule" />
    </modules>
    ...
  </system.webServer>
</configuration>

Tuesday, May 27, 2008

Adding new section to web.config - Microsoft ASP.NET

Create new section handeler for your new config section (implement IConfigurationSectionHandler interface)

Add a reference to your new config section and register it with the assembly

    public class CustomConfiguration : IConfigurationSectionHandler
    {
        public object Create(object parent, object input, XmlNode node)
        {
            /// 
            /// add your logic here ...
            /// 
            return new object();
        }
    }

Set custom configuration section in application start event(global.ascx) and add it to the cache

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
  <configSections>
    <section name="customConfiguration" 
             type="Root.Client.Configuration.CustomConfiguration, Root.Client.Configuration" />
  </configSections>
  <customConfiguration>
    .....
    //
    // your custom configuration
    //
    .....
    <customConfiguration>
</configuration>
public override void Application_Start(object sender, EventArgs e)
{
    HttpContext.Current.Cache.Insert("CustomConfiguration", 
        ConfigurationSettings.GetConfig("customConfiguration"as CustomConfiguration);
}

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...