Showing posts with label Asp.Net. Show all posts
Showing posts with label Asp.Net. Show all posts

Friday, January 10, 2014

Deploy a web application in IIS

In this article we are going to see how to deploy the web application in IIS, Now let we the steps to do this.

1. First press windows+R to get the run window



2. Type inetmgr in the run and press enter.



3. Now expand the IIS and select the Default WebSite.
4. Now right click the Default WebSite and select Add Application



5. Now Give the Alias name and select the physical path of the application and select the Application Pool.click ok





6.Expand the Default website and select the name of application that you have given as alias and right click on it.

7.Select the Manage and Browse to browse the application.









From this post you can see how to host a web application in IIS.

Wednesday, January 1, 2014

Call a Editor Template - ASP.NET MVC



           In this article we are going to see how to call a Editor template from a view,Let we take an example of Area of interest survey form which have a sequence of subjects, where user have to select the subjects got interest. when they click submit the values are submitted to the page.

EditorFor Model() Method will call the view which have the same name of model type present in the Editor Template Folder.using ADO.Net Entity Framework imports the DataModel and save it as Subject for model name.


Output



After Submit




Step 1 : Create a Controller name InfoController.
Step 2 : Create a View with strongly typed IEnumerable<Subject> Index.cshtml, which is present under the folder of the View -> Info
Step 3 : Create a View name it as Subject and save under the folder Editor Templates as Subject.cshtml
Step 4 : Create a Get and Post requests.



Sql Script:
create tablesubject
(
id int identity(1,1),
name varchar(100),
isselected bit
)

insert intosubject(name,isselected)
values('Computer Science',0)

insert intosubject(name,isselected)
values('Micro Biology',1)

insert intosubject(name,isselected)
values('Electronics',1)

insert intosubject(name,isselected)
values('Civil',0)

insert intosubject(name,isselected)
values('Aeronotics',0)

      select *from subject



C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EmpApp.Models;
using System.Text;

namespace EmpApp.Controllers
{
    public class StudiesController : Controller
    {

        EmployeeContext context = newModels.EmployeeContext();

        [HttpGet]
        public ActionResultIndex()
        {                      
            return View(context.Subjects.ToList());
        }

        [HttpPost]
        public string Index(IEnumerable<Subject> subjects)
        {
            StringBuilder builder = newStringBuilder();
            builder.Append("Result : ");

            if (subjects.Count(x => x.isselected!=null) > 0)
            {
                builder.Append(" Selected Subjects are ");
               
                foreach (Subjectsub in subjects)
                {
                    if(sub.isselected.Value)
                    builder.Append(sub.name + ", ");
                }
                builder.Remove(builder.ToString().LastIndexOf(","), 1);
            }
            else
            {
                builder.Append(" None of the subjects is selected");
            }
            return builder.ToString();
        }

    }
}




View
Subject.cshtml

@model EmpApp.Models.Subject

@Html.HiddenFor(m=>m.id)
@Html.HiddenFor(m=>m.name)
@Html.HiddenFor(m=>m.isselected)

@Html.CheckBoxFor(m=>m.isselected.Value)
@Html.DisplayFor(m=>m.name)
<br />





Index.cshtml

@model IEnumerable<EmpApp.Models.Subject>

@{
    ViewBag.Title = "Area of Interest";
}

<h2>Area of Interest</h2>

@using (Html.BeginForm())
{
   
    @Html.EditorForModel()
    <br />
    <input type="submit"value="Submit"/>
}





From this article you can learn how to call the Editor Template from the View and how to create a Editor template.



Sunday, December 29, 2013

Play a Video File from the Web - ASP.NET

In this article we are going to see how to play a video file from the web or offline, for this we are creating a application which will take the input in from the text and Play the file.

We are creating a Literal control and assign the object value at run time while play.

HTML:
<form id="form1" runat="server">
    <div>
  <br />
  <br />
  <asp:literal id="Literal1"runat="server"></asp:literal>
  <br />
  <br />
  <asp:textbox id="InputText"runat="server"width="500px"height="50px"wrap="true"
            textmode="multiLine"readonly="false">http://localhost:30687/Harivarasanam.mp4</asp:textbox>
  <br />
  <br />
  <asp:button id="Button1"runat="server"text="Play"onclick="Button1_Click"/>
    </div>
    </form>



C# Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class Player : System.Web.UI.Page
    {
        protected voidPage_Load(object sender, EventArgs e)
        {

        }

        protected voidButton1_Click(object sender, EventArgs e)
        {
            try
            {
                string mySourceUrl = this.InputText.Text;
                bool isFullSize = false;
                this.Literal1.Text = this.GetWma(mySourceUrl, isFullSize);
            }
            catch (Exceptionex)
            {
                this.Response.Write(ex.ToString());
            }
        }


        private stringGetWma(string sourceUrl, bool isFullSize)
        {
            string newtag = "";
            sourceUrl = sourceUrl + "";
            sourceUrl = sourceUrl.Trim();



            if (sourceUrl.Length > 0)
            {
            }
            else
            {
                throw newSystem.ArgumentNullException("sourceUrl");
            }

            string myWidthAndHeight = "";



            if (isFullSize)
            {
                myWidthAndHeight = "";
            }
            else
            {
                myWidthAndHeight = "width='640' height='480'";
            }



            newtag = newtag + "<object classid='CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95' id='player' " + myWidthAndHeight + " standby='Please wait while the object is loaded...'>";
            newtag = newtag + "<param name='url' value='" + sourceUrl + "' />";
            newtag = newtag + "<param name='src' value='" + sourceUrl + "' />";
            newtag = newtag + "<param name='AutoStart' value='true' />";

/* -100 is fully left, 100 is fully right. */
newtag = newtag + "<param name='Balance' value='0' />";                    

/* Position in seconds when starting. */
newtag = newtag + "<param name='CurrentPosition' value='0' />";

/* Show play/stop/pause controls. */
 newtag = newtag + "<param name='showcontrols' value='true' />";

/* Allow right-click. */
newtag = newtag + "<param name='enablecontextmenu' value='true' />";

/* Start in full screen or not. */
             newtag = newtag + "<param name='fullscreen' value='" +                                                  isFullSize.ToString() + "' />";             

             newtag = newtag + "<param name='mute' value='false' />";
           
/* Number of times the content will play.*/
 newtag = newtag + "<param name='PlayCount' value='1' />";                 

/* 0.5=Slow, 1.0=Normal, 2.0=Fast*/
newtag = newtag + "<param name='rate' value='1.0' />";         

/* full, mini, custom, none, invisible */
newtag = newtag + "<param name='uimode' value='full' />";                  

/* Show or hide the name of the file.*/
newtag = newtag + "<param name='showdisplay' value='true' />";             

/* 0=lowest, 100=highest */
newtag = newtag + "<param name='volume' value='50' />";        
newtag = newtag + "</object>";  

            return newtag;
        }
    }
}


Output:


After Play





















From this article we can see how to play a video file from web or from the offline.

Saturday, December 28, 2013

Images slideshow change the image every 1 second async in ASP.NET

In this article we are going to see how to change the image dynamically at every one second async like  image slideshow. For this we are using the script manager. To update the image async place the img control inside the update panel.

Place the files inside a Folder.Drag and drop the timer control inside the update panel and set the time interval 1000 milliseconds.

HTML

<form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1"runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1"runat="server">
        <ContentTemplate>
        <div style="width:200px;height:50px">
            <asp:Image Width="400px"Height="300px"ID="Image1"runat="server"/>
        </div>
            <asp:Timer ID="Timer1" runat="server"Interval="1000"ontick="Timer1_Tick">
            </asp:Timer>
        </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>



C#
        protected voidPage_Load(object sender, EventArgs e)
        {

        }

        protected voidTimer1_Tick(object sender, EventArgs e)
        {
            Random rand = new Random();
            Image1.ImageUrl = "~/images/"+ rand.Next(1, 8) + ".jpg";
        }


Output:


From this article you can learn how to change the image dynamically and show as image slideshow.

Wednesday, December 25, 2013

Usage of IHttpHandler and IHttpModule in ASP.Net

In this article we are going to see the usage of IHttpHandler and IHttpModule and the implementation of that in web application. Both are used for implement the preprocessing logic in request pipeline.

Following are the steps takes place while user request a page or file in a browser.

user request 
           -> BeginRequest 
           -> authenticate Request 
           -> Authorize Request
           -> PreRequestHandlerExecute
                ->  [ HttpHandler ]
           -> PostRequestHandlerExecute
           -> End Request
      
In this sample we are taking a sample that whenever a user request a file extension with .aspx we are change it to the .yv extension using HttpModule , Then write a handler for the .yv extension how to process the request of file extension .yv. This is almost like UrlRewrite sample with different extension

IHttpModule
       IHttpModule is used to implement the event based preprocessing logic, Implement the IHttpModule and Register the events which are need to execute in the module.

Two things that need to be implement is Dispose and Init in IHttpmodule

Events that are takes place in the HttpModule 

  •            BeginRequest 
  •            Authenticate Request 
  •            Authorize Request
  •            PreRequestHandlerExecute             
  •            PostRequestHandlerExecute
  •            End Request

Now we are going to register the BeginRequest event for implement the pre-processing logic. In this we are creating a log who are all giving request at which time to which file.

namespace WebApp
{

    public class CustomModule: IHttpModule
    {       
        public CustomModule()
        {

        }

        public void Dispose()
        {
          
        }

        public void Init(HttpApplication context)
        {                       
            /* Register a Event of a Request PipeLine */
            context.BeginRequest += new EventHandler(context_BeginRequest);
            context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
        }

        void context_PreRequestHandlerExecute(object sender, EventArgse)
        {
         
        }

        void context_BeginRequest(objectsender, EventArgs e)
        {
            HttpApplication appli = sender as HttpApplication;
            StreamWriter logwriter = newStreamWriter(appli.Server.MapPath(@"~\Files\log.txt"),true);
            logwriter.Write("Request has been raised at "+DateTime.Now.ToString()+" for "+appli.Request.RawUrl);
            logwriter.Close();
           
            string url = appli.Request.RawUrl;
            if (url.Contains(".aspx"))
            {
                string rewriteurl = url.Replace(".aspx", ".yv");
                appli.Response.Redirect(rewriteurl);
            }

        }
} 

Log Information:
Request has been raised at 25-12-2013 19:40:01 for /AddEmployee.aspx

Request has been raised at 25-12-2013 19:40:04 for /AddEmployee.yv

IHttpHandler 
       IHttphandler is used to implement the preprocessing logic based on the extension of requested file type, implement the IHttpHandler by implement the ProcessRequest and IsResuable in the class.

In this handler we are going to handler the request of file type with extension .yv.

namespace WebApp
{

   public class CustomHandler:IHttpHandler
    {
        public boolIsReusable
        {
            get { return true; }
        }

        public voidProcessRequest(HttpContext context)
        {
            context.Server.Transfer(context.Request.RawUrl.Replace(".yv",".aspx"));
        }
    }

} 

Web.Config
    In web.config we have to specify the settings in two place one is in System.web and another in system.WebServer, because if you not specify in system.Webserver it will not reflect in IIS only reflect in visual studio development server.please specify  the runAllManagedModulesForAllRequests  as True

<system.web>
   <httpHandlers>   
      <add verb="*" path="*.yv" type="WebApp.CustomHandler, WebApp"/>     
    </httpHandlers>
    <httpModules>
      <add name="UrlReWrite" type="WebApp.CustomModule, WebApp"/>
    </httpModules>
  </system.web>

<system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
 
   <modules runAllManagedModulesForAllRequests="true">
      <remove name="ScriptModule"/>
      <add name="UrlReWrite" type="WebApp.CustomModule, WebApp"/>    
    </modules>
   
 <handlers>      
      <remove name="extensionhandler"/>
      <add name="extensionhandler" verb="*" path="*.yv" type="WebApp.CustomHandler, WebApp"/>     
    </handlers>
  </system.webServer>


Output:
     In the below image you can see that the user request a url with file extension of .aspx , but it changes to .yv and the file with .yv is processed successfully by handler.Third display shows the output.




From this article you can learn how to implement the IHttpHandler and IHttpModule and what is the usage of that in web application.