Posts Tagged ‘ASP.NET’

Automatic control in ASP.NET MVC

Thursday, September 2nd, 2010

Although the convention proposed by the ASP.NET MVC framework help us to structure our applications and, in most cases, be more productive, occasionally also require us to enter repetitive code to comply with the proposed standard.

For example, in the case of controllers with actions that return the default view, we usually use a code like the following:

public class HomeController: Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Create()
    {
        return View();
    }   

    public ActionResult Edit()
    {
        return View();
    }

    public ActionResult Delete()
    {
        return View();
    }

}

It’s not terrible, but drivers with many actions of this type can be a bit heavy … can not we do something to improve this?
Automatic controllers

Looking at the driver code above, we see that the execution logic is very simple: every action returns the user implemented view as rightful convention. Obviously this is a widespread behavior may be playing with the extension mechanisms of the framework.

A very quick way to do it is to overwrite the method HandleUnknownAction() class Controller , described here some time ago , which allows us to process the requests made to non-existent shares in the controller.

Remember that when a request comes to a controller, running the method whose name matches the action invoked, in the absence of the framework call HandleUnknownAction() , allowing us to take control of the situation. In our case, we could introduce the logic in this method to return the user, automatically, a view whose name matches the action, following the standard naming convention.

To do this, simply create a base class called AutoController , and enter the following code to have the problem solved:

public class AutoController : Controller
{
    protected override void HandleUnknownAction(string strActionName)
    {
 
        //  Try to find a view with the name of the action ...
 
        ViewEngineResult viewResult = ViewEngines.Engines.FindView
                            (this.ControllerContext, , null) ;
 
        if (viewResult.View != null)
        {
            View(strActionName).ExecuteResult(ControllerContext);
            return;
        }
 
        // If we have not found anything, we follow the
        // Default behavior ...
 
        base.HandleUnknownAction(strActionName);
    }

Simple, no? All we do in the code is to use the library ViewEngines to seek a view whose name matches the action you are trying to run, returning it to the user if possible locate it.

If you can not find a view for the action invoked, will run the default treatment for this situation, which is nothing more than throwing an exception of type HttpException with error 404 (not found).

That’s it! From this point, all the driver classes that inherit from AutoController include this behavior, so it will be possible to avoid the implementation of methods whose sole mission is to return the default view as convention.

For example, the driver that we wrote at the beginning of this post might be as follows, much more compact:

public class HomeController: AutoController
{
}

But beware if that is not gold that glitters …

However, before using this technique we must be clear what it really means to not run into unpleasant surprises.

Each request received by the controller is not explicitly implemented will be processed with the operator introduced in HandleUnknowAction() without passing through any type of filter, or send any information in the ViewData .

For example, it would be possible to access the view directly, only know her name, which in some scenarios may be dangerous from the point of view of system security.

Obviously, the technique is also not valid at times when the sight expect to receive some form of driver information (like data view, or indication of use of a specific master page), or when the action must be decorated with a filter .

In these cases, these concrete actions should continue to be implemented explicitly in the controller, but the rest can continue to be processed automatically:

public class HomeController: AutoController
{
    // GET /Home/Employers
    //  Only for registered users
    [Authorize]
    public ActionResult Employers()
    {
        return View();
    }
 
    // GET /Home/Salary
    public ActionResult Salary()
    {
        var varAppServices = new AppServices();
        return View(varAppServices.GetReferences());
    }
 
    // GET /Home/{ExamplePage}
    // Default processing, returns the view {ExamplePage}
}

In summary, in this post we have studied a technique that allows us to create drivers capable of providing a default for all requests made to the same, saving the writing of actions that simply return the default view.

And although, like everything else, practice has limitations and dangers, its use can be interesting websites without major safety requirements, such as public information websites, whose drivers are mostly of the type described.

Processing requests for actions not available in ASP.NET MVC

Sunday, August 22nd, 2010

ASP.NET MVC controllers that inherit from the Controller can easily process the requests made to actions not defined. To do this, all you have to do is override the method HandleUnknownAction() and implement the logic that we want to run in these cases.

In the following code, the requests made to /Home/Index and /Home/About will be processed normally, but /Home/ActionPage will be processed by HandleUnknownAction , whose implementation will show the view “Index” with a personalized message:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewData["Message"] = "Welcome to ASP.NET MVC!";
        return View();
    }

    public ActionResult About()
    {
        return View();
    }
protected override void HandleUnknownAction(string strActionName)
(
ViewData ["Message"] = "Are you trying to" + strActionName + "?"
View("Index").ExecuteResult(this.ControllerContext);
)
)

Happy Programming!! ;-)

More javascript on the web: Microsoft Ajax CDN

Thursday, September 17th, 2009

ASP_Ajax

A few days ago said the possibility of using the infrastructure of Google to host JavaScript libraries for our applications. Well, now that Microsoft has launched a similar service, Microsoft Ajax CDN , A content distribution network where we can download the runtime libraries of scripts that we use in our applications.

Or in other words, we can make free use of these libraries, without limitation of bandwidth and regardless of whether or not for commercial purposes. Just be referenced from your code:

?View Code ASPNET
<script src="http://ajax.microsoft.com/ajax/jquery-1.3.2.min.js" 
        type="text/javascript"></script>

The main advantage of this method is the speed with which these files will be served, since it uses the infrastructure of the Redmond giant, while the cache is shared with other websites that are also used. It also provides the ability to use scripting to Web sites that do not have permission to upload files (such as platform blogger)

Unlike the Google service, since this CRC only time we can find the libraries that are officially part of the Microsoft development platform, such as those typical of ASP.NET Ajax, jQuery and those plugins that are added. The address http://www.asp.net/ajax/cdn/ can see the complete list of libraries, with their corresponding addresses download.

Additionally, Scott Guthrie said in his blog that the new control ScriptManager that come with ASP.NET 4.0 includes a property called EnableCdn which will activate the download of the Ajax libraries and all those necessary for the operation of controls, directly from their servers.

AjaxCDN_EnableCdn_160909

The drawbacks, as the same as the Google service: If you do not have network connectivity in development time, we really have it raw.

More information: http://www.asp.net/ajax/cdn/

Collection of free chapters of books on ASP.NET MVC

Monday, September 7th, 2009

I received some messages from readers Wisecodes who want to begin in ASP.NET MVC, but do not know where to start, and I suggest you write some posts that explain this new framework from scratch. Perhaps someday it, but meanwhile, I reminded them that there are free resources that can be very useful to take the first steps (and even dig a little) on this technology.

For example, recently have appeared a large number of books on ASP.NET MVC, and most of them can download free chapters, in addition to help us decide which of them might be in our interest to have the information value we can provide. Like always, the issue is the spread of this information, so I decided to make this compilation to have them all by hand. As you’ll discover new pieces expand the list, and of course, if you know someone who is not here, I’ll comment’s and add it.

That yes, all in English, to be very technical but I think they are easy to understand.

Pro ASP.NET MVC Framework Pro ASP.NET MVC Framework
Author: Steve Sanderson
Post-show: Now Published Pro ASP.NET MVC Framework (Apress) «Steve Sanderson’s blog
Free Chapter: Chapter 2: Your First ASP.NET MVC Application (23 pages) In this chapter, the author shows in great detail, almost step by step, how to create our first simple application with ASP.NET MVC. First, it creates a very basic project through which explains the basics of programming by following this model, and then goes into the creation of a mini-application data entry, validations and simple logic.
Professional ASP.NET 1.0 MVC Professional ASP.NET 1.0 MVC
Authors: Scott Hanselman Rob Conery, Phil Haack and Scott GuthrieFree

Chapter: Chapter 1: Nerdinner (192 pages)
This extensive chapter describes the creation of a moderately complex site, www.nerddinner.com, using large amounts of related technologies such as filters MVC framework, Ajax, LinqToSql, unit testing, etc.. Highly recommended, essential.

ASP.NET MVC 1.0 Quickly ASP.NET MVC 1.0 Quickly
Author: Maarten Balliauw
Post-show: Announcing my book, ASP.NET MVC 1.0 Quickly
Free Chapter: Chapter 2: Your first ASP.NET MVC application (20 pages)
A brief tour of the process of creating an MVC application, the system routes, controllers, views, and unit testing. Pretty basic, perhaps too “quickly”, but valid in any case.
PASP.NET MVC in Action ASP.NET MVC in Action
Authors: Jeffrey Palermo, Ben Scheirman, Jimmy Bogard
Post-show: Announcing ASP.NET MVC in Action (from Manning) – Jeffrey Palermo (. Com) – CodeBetter.Com
Free resource: Getting Started with the ASP.NET MVC Framework (Green Paper – PDF) (18 pages) (The link to the document you sent by email after submitting your address.) This is another introduction to the framework, the system routes, controllers and views from scratch.Free Chapter: Chapter 9: AJAX in ASP.NET MVC (21 pages)
Interesting introductory chapter to the use of Ajax technology on ASP.NET MVC using jQuery and Ajax helpers to exchange data with the server.
ASP.NET MVC Framework Unleashed ASP.NET MVC Framework Unleashed
Author: Stephen Walther
Free chapters: the author has published several chapters in his blog, and withdraw as soon as the book is available at Amazon, so hurry up that the contents are excellent.
Chapter 1 – An Introduction to ASP.NET MVC
Chapter 2 – Building a Simple ASP.NET MVC Application
Chapter 3 – Understanding Controllers
Chapter 4 – Understanding Views
Chapter 5 – Understanding Models
Chapter 6 – Understanding HTML Helpers
Chapter 9 – Understanding Routing
Beginning ASP.NET MVC 1.0 Beginning ASP.NET MVC 1.0
Authors: Simone Chiaretto, Keyvan Nayyeri
Free Chapter: Chapter 9: Testing ASP.NET MVC Applications (38 pages)
Interesting chapter describes various techniques for unit testing of applications built with this framework, including the creation of mocks, dependency injection controllers, testing route and obliquely, some TDD.

Software company promises to plant a tree for every bug they have

Wednesday, September 2nd, 2009

TreesForBugs

The company Kentico Software announced a very interesting initiative called “Tree for Bugs”. As explained in a press release, will plant a tree for every bug that has its software. Good for the ecology and also for the marketing strategy that has tenido. Personally as a developer, could collaborate with thousands of bugs selflessly.
Url: http://trees.kentico.com

Press Release:
Kentico Software, the Web content management system vendor, today announced it will plant a tree for every bug found in the latest version of Kentico CMS for ASP. NET. The company also promises to fix all bugs reported within 7 days.
Nashua, NH (PRWEB) August 30, 2009 – Kentico Software will plant a tree for every bug in Kentico CMS 4.1 reported by clients and it the “We are very confident about the quality of our Web Content Management platform. Although we can hardly Eliminate all bugs, our goal is to minimize their number and fix them within 7 business days, so that We can provide a stable and reliable solution to our clients, “explains Petr Palas, Kentico CEO. “We want To encourage our clients to report all bugs they encounter. And We decided to give back something that everyone on the planet will benefit from – new trees, “he adds.
“We are very confident about the quality of our Web Content Management platform. Although we can hardly Eliminate all bugs, our goal is to minimize their number and fix them within 7 business days, so that We can provide a stable and reliable solution to our clients, “explains Petr Palas, Kentico CEO. “We want To encourage our clients to report all bugs they encounter. And We decided to give back something that everyone on the planet will benefit from – new trees, “he adds.
“Kentico is great to work with, it’s a reliable and stable CMS that has been developed using best practice techniques and is thoroughly tested.” Said Andy Dale, Senior Web Developer at Last Exit, the leading interactive agency in London, UK “As version 4.1, it is a mature product with the features and a support level that has helped us to deliver a wide range of successful websites. ”
The company will publish photos of the planted trees in December. They will plant at least 100 trees, Although they expect that the number of reported bugs will be much lower. All information about the initiative is available at http://trees.kentico.com.

ASP.Net Gridview to CSV, Excel

Saturday, April 25th, 2009

This method, export to Excel file from Gridview

VB.NET


Sub wc_gvGridviewToExcel(ByVal gvGridview As Gridview, ByVal strExcelFilePath As String)

'Columns

Dim intColumnsCount As Integer = gvGridview.Columns.Count - 1

'Header

Dim strHeaderNames As String = Nothing

For i = 0 To intColumnsCount

strHeaderNames += gvGridview.Columns(i).HeaderText & ";"

Next

WriteToExcel(strHeaderNames, strExcelFilePath)

'Each Row

Dim strRowText As String = Nothing

For Each grdRow As gvGridviewRow In gvGridview.Rows

For i = 0 To intColumnsCount

strRowText += grdRow.Cells(i).Text & ";"

Next

WriteToExcel(strRowText, strExcelFilePath)

strRowText = Nothing

Next

End Sub

Sub WriteToExcel(ByVal strText As String, ByVal strExcelFileName As String)

Dim myFileWriter As New System.IO.StreamWriter(strExcelFileName, True)

myFileWriter.WriteLine(strText)

myFileWriter.Close()

End Sub

C#.NET

public void wc_gvGridviewToExcel(Gridview gvGridview, string strExcelFilePath)
{

 //Columns

 int intColumnsCount = gvGridview.Columns.Count - 1;

 //Header

 string strHeaderNames = null;

 for (i = 0; i <= intColumnsCount; i++) {

 strHeaderNames += gvGridview.Columns(i).HeaderText + ";";
 }

 WriteToExcel(strHeaderNames, strExcelFilePath);

 //Each Row

 string strRowText = null;

 foreach (gvGridviewRow grdRow in gvGridview.Rows) {

 for (i = 0; i <= intColumnsCount; i++) {

 strRowText += grdRow.Cells(i).Text + ";";
 }

 WriteToExcel(strRowText, strExcelFilePath);

 strRowText = null;

 }
}

public void WriteToExcel(string strText, string strExcelFileName)
{

 System.IO.StreamWriter myFileWriter = new System.IO.StreamWriter(strExcelFileName, true);

 myFileWriter.WriteLine(strText);

 myFileWriter.Close();
}

Happy Programming!!

Free eBooks For Beginner Programmers

Wednesday, March 11th, 2009

1) ASP.NET MVC

A strong 195 pages with chapters of Wiley Publishing published book “Professional ASP.NET 1.0 MVC” is a preliminary version in PDF format. The book was written by renowned authors Rob Conery, Scott Hanselman, Phil Haack and Scott Guthrie.

The free chapter gives a detailed example of the project NerdDinner, a website to plan and arrange joint dinner. It is shown how this small but complete application built with ASP.NET MVC. The source code can be found on Codeplex (http://nerddinner.codeplex.com) .

Click here to download.

2) Microsoft Visual Studio 2008 EBooks (LINQ, Silverlight 2 and ASP NET 3.5)

Microsoft hass published interesting three books. Great!

  1. Introducing Microsoft LINQ
  2. Introducing Microsoft ASP.NET AJAX
  3. Introducing Microsoft Silverlight 1.0

Click here to download.

3) Understanding Microsoft Virtualization Solutions

Microsoft Press has published “Understanding Microsoft Virtualization Solutions – From the Desktop to the Datacenter” now available as free downloads. Great! ! Microsoft seems to have missed the virtualization pattern gradually came to really try.

Click here to download.

4) 7 Development Projects for Microsoft Office Sharepoint Server 2007 and Windows Sharepoint Services Version 3.0

Click here to download.

Intro to ASP.NET Maker, PHPMaker, ASPMaker, JSPMaker, XML/XSLT Maker & CFMMaker.

Monday, March 9th, 2009

ASP.NET Maker : Generate the asymmetric multiprocessing system. Net 2. 0 writes the script from the database

ASP.NET Maker is a powerful yet easy-to-use ASP.NET code generator for ASP.NET 2.0. It can create a full set of ASP.NET 2.0 pages quickly from Microsoft Access, Microsoft SQL Server, MySQL, Oracle and other ODBC data sources. You can instantly create Web sites that allow users to view, edit, search, add and delete records on the Web.
ASP.NET Maker offers numerous useful features, including 3-Tier Architecture, ASP.NET AJAX, Extended Quick Search, Drill-down Master/Detail, Advanced Security, User Registration System, Custom View, Report, Export, File.

Better Quality Video Tutorial: Click Here

Read More..

PHPMaker: Generate PHP from MySQL

PHPMaker is the powerful automation equipment which can generate all matching of PHP directly from the database of MySQL, (because of Windows). Using PHPMaker, in order to compile because the user searches at once, in order to add, it makes that you see, possible, deleting the record of the net data type everything of MySQL, including BLOB, ENUM and set can draw up the web sight which it supports.

Read More..

ASPMaker: Generate the asymmetric multiprocessing system from the database.

ASPMaker the asymmetric multiprocessing system (the be active server page) is the powerful automation equipment which can generate all matching directly from the database of the Microsoft access or data source which supports the noise. Using ASPMaker, at once the user looks at the record of the net, compiles, searches, adds, can draw up the web sight which makes that it deletes possible.

Read More..

JSPMaker: JSP is generated directly from MySQL or Oracle.

JSPMaker however JSP (the page of JavaServer) being powerful draws up all matching directly from MySQL or Oracle, is the cord/code generator which it is easy to use.

Read More..

XML/XSLT Maker: The lattice of DHTML and dynamic XML/XSLT draw up the web sight which it has made be based

As for XMLMaker the lattice of DHTML for the general user and dynamic XML/XSLT for the developer of time it is the cord/code generator which cannot draw up the web sight which it has made be based. The lattice of DHTML is the WEB based lattice part which is based on conduct and XML/XSLT of DHTML, but that the lattice is used, knowledges of XML/XSLT/DHTML is not required.

Read More..

CFMMaker: Generate the template of CFM from the database.

CFMMaker however CFM (the template of ColdFusion) being powerful draws up all matching directly from your data source, is the cord/code generator which it is easy to use. The database which is supported makes a noise and or connection characteristic Microsoft of ODBC access, the Microsoft SQL server, includes Oracle or the database.

Read More..

Mapping .html file to ASP.NET

Tuesday, February 24th, 2009

Step one: Open IIS, in your site to set up virtual directory property page tab, click on the “Application Settings (Application Settings)” under the “configuration (Configuration..)” Button, open the “application configuration (Application Configuration ) “window” App Mappings (App Mappings) “Tab page, click on the” Add (Add..) “button, the pop-up” Add / Edit Application Mapping suffix (Add / Edit Application Extension Mapping ) “window, in the” executable program (Executable) “the right of the text box, type C: \ WINNT \ Microsoft.NET \ Framework \ v1.1.4322 \ aspnet_isapi.dll (file path will be. net framework version of the different different for the sake of safety, you can in the “App Mappings (App Mappings)” tab under the list of double-mapping “. aspx”, in the pop-up window to select its “executable program (Executable)” text Copy the text box and then out), in the “name suffix (Extension)” text box you want to add the suffix of “. html”, then click OK to save all open windows to complete the property settings in IIS; 

Step two: open you want to configure the site or virtual directory under the root directory of the web.config file, configuration section in <system.web> add the following configuration sections: 

<httphandlers>
<add verb=”*”
path=”*.html”
type=”System.Web.UI.PageHandlerFactory” />
</httphandlers>


Save web.config file, Now we are set !!! :-)

Configuring Temporary Asp.Net Files Folder

Tuesday, February 24th, 2009

The Temporary ASP.NET Files folder contains all temporary files and assemblies being created to serve pages and resources. You have to look into this subtree to find dynamically created files for your Web pages. Note that the Temporary ASP.NET Files directory is the default location for dynamically created files, but this location is configurable on a per-application basis using the section in the web.config file:

<compilation tempDirectory="d:\MyTempFiles" />

Given below is figure illustrates the default location for the temporary ASP.net folder

asp_temp

Source : Click here…