Lets Have a fun with Technology.

BTemplates.com

Follow by Email

Friday, March 25, 2016

Significant of routes.IgnoreRoute in MVC Application.


Hello,

In MVC Application, RouteConfig.cs file have a RegisterRoutes method.

This method is give facility to not process some files by the asp.net mvc engine.




public static void RegisterRoutes(RouteCollection routes)

{

  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");



}


This will match requests to resources like /Manager.axd or /a.axd/b/c.aspx,
but will not match a request to any *.axd resource below the root of your site like /a/b/c.axd

The following works for files in the root:



routes.IgnoreRoute("{file}.css");

routes.IgnoreRoute("{file}.jpg");

routes.IgnoreRoute("{file}.gif");


If you want to ignore files in a specific directory, you can do this:



routes.IgnoreRoute("Folder/{*pathInfo}");


Default Start page in MVC Application


Hello

I have seen many developer looking for How to set default page in MVC application.

In MVC Application you do not set start page by clicking on web page.  
From RouteConfig.cs file you can do it.
See below screen shot.


Now suppose you want to saw this StartPage action when application is default loaded.


Please note here you have to keep empty URL so it will take default page and route default page to  StartPage action of ketan controller.

Thursday, March 24, 2016

Important of RouteConfig.cs in MVC


Hello,

Lets see important of RoutConfig.cs file in MVC Application.

if you look at your MVC application, you will find RouteConfig.cs file in App_start folder.

in RouteConfig.cs file you will find the method name RegisterRoutes. This method is use to add routing information in RouteCollection.

This RegisterRoutes method is call from Global.asax file. 




    RouteConfig.cs file

    


RouteConfig.cs is very important file in MVC Application.

Here we do mapping  user friendly URL ( for end user) with controller name and action.

route.MapRoute add the routing information in RouteCollection.
In Maproute first argument is name. which must be unique name.
Second argument is URL format. 
Third argument is Controller, Action and Argument.

Now lets take example of I am creating Ketan Controller and Ketan View. We will give user friendly URL to end user.








In MVC Application you do not set start page by clicking on web page.  
From RouteConfig.cs file you can do it.
See below screen shot.


Now suppose you want to saw this StartPage action when application is default loaded.


Please note here you have to keep empty URL so it will take default page and route default page to  StartPage action of ketan controller.

There are much more we will do with RouteConfig file. I will discuss it later.



What is ActionResult in ASP.Net MVC


ActionResult is return type of a controller method in MVC.




public ActionResult Index()

        {

            return Content(DateTime.Now.ToString());



        }



Below are different response types which can be sent to the view via ActionResult:-

ViewResult - Renders a specified view to the response stream

EmptyResult - An empty response is returned

PartialViewResult - Renders a specified partial view to the response stream

FileContentResult - Returns a file to the client

RedirectResult - Performs an HTTP redirection to a specified URL

RedirectToRouteResult - Performs an HTTP redirection to a URL that is determined by the routing engine, based on given route data

JsonResult - Serializes a given object to JSON format

JavaScriptResult - Returns a piece of JavaScript code that can be executed on the client

ContentResult - Writes content to the response stream without requiring a view

FileStreamResult - Returns a file to the client, which is provided by a Stream

FilePathResult - Returns a file to the client

Wednesday, March 23, 2016

Implement Caching in MVC Application.


Hello,

Caching is important in all web application.

MVC Provide better way to do this. 

Lets create simple caching application in MVC. This application store Datetime in Cache.


First create sample application as shown in below article.


http://dotnet-magic.blogspot.in/2016/03/how-to-create-simple-mvc-application.html


Now add below code in controller. It will return current system datetime.




      public ActionResult Index()

        {

            return Content(DateTime.Now.ToString());

        }

Now run your application. it will display current date time. Each time if you refresh your web page it will display current date time of system.





Now Lets implement caching in this application.

Change your code as shown below




[OutputCache(Duration = 300, VaryByParam = "none")]
public ActionResult Index()
        {
            return Content(DateTime.Now.ToString());
        }

Now run your application. it will show current system date time . After some time (Before 5 min. Duration is 5 minute) again refresh it. it will still show old time. This is magic of Caching.

Now look at above declaration. You will find VaryByParam property.
What;s a significant of this property. Well We may store whole master in cache. For example Country master, state master etc.. Now suppose we see details of one country then this property will helpful. in VaryByParam we will provide countryid as parameter. so it will filter from countryID.





[OutputCache(Duration = 300, VaryByParam = "countryID")]
public ActionResult GetCountryDetail(int contryID)
        { 
            Country c = new Country()
            return Content(c.getCountryDetails(countryID));
        }

Now you have question where this cache data is stored. However we have property to specify this. You can store data either on client machine or server machine or proxy server.

"Location" is property for this. Below is different value for this property.




For different option description look at blow article.

https://msdn.microsoft.com/en-in/library/system.web.ui.outputcachelocation(v=vs.110).aspx

Lets me know if you have question in this simple implementation.

How to create simple MVC Application using visual studio 2015 (Empty MVC Template)


Hello,

Below are steps to create simple MVC application using visual studio 2015 (Empty MVC Template)


1.  Go to File -> New -> Project

     In Visual C# click on web and add Asp.Net Web Application as shown below.



 2. This will open "select a template" window. Select Empty template and Click on MVC as shown below.



3. This will create a simple application like shown below.



4. Now Next step to add simple controller.

   Right click on Controller folder.  then go Add -> Controller option.

   

5. Give it to name "HomeController"



6. Now we will create simple view.
   Which associate with this controller.

   Right click on View Folder.
   Now click on Add -> View
   Give View name is Index as shown below
    
 


 7. Now in View add below text in Body tag.

    This is First Application in Visual Studio 2015

   



Your Application is ready. Press F5 and Enjoy.


Monday, March 14, 2016

ASP.Net 5 New Features (Asp.Net Core 1.0 New Features)


Below are new features of ASP.NET 5.

Open Source
Now ASP.NET is now open source.We can use the deployment code for ASP.NET which will be found on GitHub.
No license is require to use this feature.

Cross Platform
Before ASP.Net 5, asp.net application only run on windows.
But asp.net 5 provide cross platform so now your application run on MAC, Linux.
This is very good feature of asp.net 5

Platform Independent Hosting
Now your asp.net application don't rely on IIS for hosting. Now you can host your application on IIS or Self hosting.

Now you can also host your application on any system because in ASP.NET, Core CLR can deploy the all dependencies with your application’s deployment package.

There is no longer a requirement for other dependencies to run the ASP.NET 5 application

using ASP.NET 5, now you can host your application on any hosting platform which will be running on any type of device. You don’t need to worry about hosting.

Dependency Injection

In ASP.NET 5, Dependency Injection is used at framework level. Actually ASP.NET 5 is designed in such way which support leverage dependency Injection.

To use Dependency Injection in ASP.NET, you need to use the built in dependency injection container. It is build into ASP.NET 5 framework helps in giving the right services for your environment. 

Dynamic Compilation
ASP.NET 5 comes with attractive feature which can change the life of developers as well as increase their productivity. As we did earlier, make the changes in the code and save and go to browser and refresh the page for loading the latest contents. 

But with ASP.NET 5, you don’t need to refresh the page, using dynamic compilation features the CLR will compile your code in backend and refresh the content as well. So, this makes your work faster and efficient. 

Deployment on Cloud
Now you can deploy your asp.net application on cloud. No additional changes require to deploy asp.net application on cloud.

Make Application Faster using HTTP Pipeline.
Previously we use the HTTP Pipeline where all the components were added by default. But ASP.NET 5 provides the flexibility to choose your required components and add them with HTTP Pipeline. You can also remove those components from the HTTP Pipeline which are not useful for your application. This makes your application faster.



Sunday, March 13, 2016

How to stop Bubbling on click event.


How to stop Bubbling on click event.

Hello,

It may case you want to kill bubbling on the click event.

JQuery will help you to achieve this by event.stopPropagation().

Below is example of kill the bubbling on button click.





$(document).ready(function()

{

$("#<%= btnArrow.ClientID %>").on ('click', function(event){event.stopPropagation();});

//Kill the bubbling

});




You may call event.stopPropagation() from function.

For Example below I am calling RadPrompt.




radprompt('Enter Name', promptCallBackFn);



function promptCallBackFn(arg) {

//Do your code.

event.stopPropagation();

//This will prevent Bubbling.

}