Archive for the ‘ASP.NET’ Category

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/

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…

ASP.NET MVC 1.0 Release Candidate Now Available

Friday, February 20th, 2009

Today we shipped the ASP.NET MVC 1.0 Release Candidate (RC).  Click here to download it (note: the link just went live so if it isn’t working wait a few minutes for the server you are hitting to refresh).  It works with both Visual Studio 2008 and Visual Web Developer 2008 (which is free).

Today’s RC is the last public release of ASP.NET MVC that we’ll ship prior to the final “1.0” release.  We expect to ship the final ASP.NET MVC 1.0 release next month.

In addition to bug fixes, today’s build includes several new features.  It also includes some refinements to existing features based on customer feedback.  Please read the release notes that ship with the ASP.NET MVC download for full details on all changes.  The release notes include detailed instructions on how to upgrade existing applications built with the ASP.NET MVC Beta to the RC.

Read More.. 

WCF vs Webservices

Thursday, February 12th, 2009

How to choose between ASP.NET Web Services and WCF Services for Services?

ASP.NET Web Services are a good choice for simple HTTP-based services hosted in IIS.

WCF is a good choice if you need the performance of TCP communication over HTTP or if

you need to host the service without a Web server. WCF provides support for WS*, which

includes support for end-to-end security and reliable communication. WCF allows you to

implement duplex communication, and you can also use it with Windows Message Queuing

and as a Windows service. In addition, you have more options with regard to protocols,

bindings, and formats. Keep in mind that WCF requires .NET 3.0 or higher.