Wednesday, August 25, 2010

Accessing Rss Feeds

RSS is a format for an XML document that describes a list of items, such as blog entries or news headlines. The document may just be a stream of XML created on the fly rather than a static file. In this post, we’ll tell you to put an RSS document (using Version 2.0) on a page by using the XmlDataSource and DataList controls.

Well what you need for this are as given below

  • URL to a valid rss file it could be .xml,.rss,.aspx.,.php etc
  • kind of schema of that rss file we call Xpath, to reach upto the content you are interested in.

A typical Rss file is like this.

A typical schema is like this

<rss>
<channel>
<item>
<title></title>
<description></description>
<link></link>
<pubDate></pubDate>
<!--...-->
</item>
</channel>
</rss>

XPath rss/channel/Item tell that this is Item from where you want to start fetching from.

Now i guess enough theoretical content has been described.

let’s start working.

Create a web site,

in default page or wherever you want to display the feeds put xmldatasource.

in navigationurl put the url of the rsss file. one example is (http://www.spaces.msn.com/members/mauliksoni/feed.rss). if you are choosing this Rss then put XPath=”rss/channel/item”.

take a data list and set its DataSourceID to the id of XmlDataSource you have taken.

in datalist set enableViewState to false.

Item template would be like this.

<ItemTemplate>
              Title:<%# XPath("title") %><br /><br />
              Description:<%# XPath("description")%>
              <hr />
           </ItemTemplate>

 

XPath(“Tiitle”) will print content of the Title tag and Xpath(“description”) will print content inside Description tag.

complete markup code is like this

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:XmlDataSource ID="XmlDataSource1" runat="server"
            DataFile="http://www.spaces.msn.com/members/mauliksoni/feed.rss"
            XPath="rss/channel/item"></asp:XmlDataSource>
        <asp:DataList ID="DataList1" runat="server" DataSourceID="XmlDataSource1" EnableViewState="False">
            <ItemTemplate>
               Title:<%# XPath("title") %><br /><br />
               Description:<%# XPath("description")%>
               <hr />
            </ItemTemplate>
        </asp:DataList>
    </div>
    </form>
</body>
</html>

Run the project and see the feeds from your favorite site. Enjoy.

Monday, August 16, 2010

Generating Views

Inno design

Hello Guys ,

How is Your ride with asp.net mvc going  ?? :-)

Let's Understand the Views...

The set of views in an ASP.NET MVC application is the public face of the application. ASP.NET MVC views are responsible for rendering the HTML pages that people see when they visit your website.

Creating a View

The easiest way to create a new view is to create the view from an existing controller action. You can right-click anycontroller action within the Visual Studio Code Editor window and select the menu option Add View to create a new view automatically.

When you select the Add View menu option, the Add View dialog opens like below. This dialog enables you to set various view options. For example, you can specify whether a view uses a view master page.

For example, if you open a controller named Person and right-click the Index() action to add a view, and you leave the default options selected in the Add View dialog, you’ll get the view in like below Listing :

The above Listing looks almost like a standard HTML document. This view contains two <asp: Content> tags. Any content that you place within the first <asp: Content> tag appears in the <title> tag of the resulting HTML document. Any content that you place within the second <asp: Content> tag appears in the <body> tag of the resulting HTML document.
For example, the modified Index view in the below Listing  has been modified to display the current time.

Above Listing contains familiar HTML tags such as the <h1 > and <p> tags. You can put anything that you would put in a normal HTML page within a view including images, iframes, Java applets, Flash, and Silverlight. The view also contains a script that displays the time. The expression DateTime. Now. ToString("T") returns the current time.
You embed a script in a view by using the <% %> script delimiters.The <%= %> script delimiters are shorthand for <% Response. Write %>. You can use the <%= %> script delimiters to write the value  of an expression to the browser. The following two scripts do exactly the same thing:
<%= DateTime. Now. ToString("T") %>
<% Response.Write(DateTime. Now.ToString("T") ); %>
We'll Understand view in detail in the upcoming posts....Till than Generate your simple views and enjoy Mvc'ing... :-)
Hope this helps...

Sunday, August 15, 2010

Understanding Controllers and Actions


Hello Guys,
It has been so long we wrote on Asp.Net MVC….Sorry for the dis-continuity…But here we are , lets understand the controllers and actions…
ASP.NET MVC controllers are responsible for controlling the flow of application execution. When you make a browser request against an ASP.NET MVC application, a controller is responsible for returning a response to that request.
Controllers expose one or more actions. A controller action can return different types of action results to a browser.
For example
–> a controller action might return a view,
–> a controller action might return a file, or
–> a controller action might redirect you to another controller action.
Creating a Controller
The easiest way to create a controller is to right-click the Controllers folder in the Visual Studio Solution Explorer window and select the menu option Add, Controller.
Selecting this menu option displays the Add Controller dialog :
PersonController
If you enter the name PersonController, you get the code in below listing :
Controllers\ProductController.cs
codecontroller
Note : A controller name must end with the suffix Controller. If you forget to include the Controller suffix, you can’t invoke the controller.
Notice that a controller is just a class (a Visual Basic or C# class) that inherits from the base System.Web.Mvc. Controller class. The controller class in above Listing exposes one action named Index(). The Index() action is
the default action that is invoked on a controller when no explicit action is specified.
Notice that the Index() action returns an ActionResult. A controller action always returns an ActionResult (even if it doesn’t appear to be returning an ActionResult). The ActionResult determines the response returned to the browser. The Index() controller returns a view as its ActionResult.
A controller typically exposes multiple actions. You add actions to a controller by adding new methods to the controller. For example, the modified Person controller in the below Listing exposes three actions named Index() , Help(), and Details() .
updatingcontroller
Here’s what you would type into a browser address bar to invoke the different actions:
  • /Person/Index — Invokes the PersonController Index( ) action
  • /Person — Invokes the PersonController Index() action
  • /Person/Help — Invokes the PersonController Help() action
  • /Person/Details/34 — Invokes the PersonController Details() action with the value 34 for the Id parameter
You invoke a controller action by following a particular pattern that looks like this:
{controller}/{action}/{id}
Notice that when you invoke a controller, you don’t include the Controller suffix in the URL. For example, you invoke the Person controller with the URL /Person/Index and not the URL /PersonController/Index.
The default controller action is the Index() action. Therefore, the URL /Person/Index and the URL /Person both invoke the Person controller Index( ) action.
When you invoke a controller, you can supply an optional Id parameter. For example, the Details() action accepts an Id parameter. The URL /Person/Details/2 invokes the Details() action and passes the value 2 for the Id parameter. The name of the parameter is important. You must name the parameter Id.
Note : The default pattern for invoking controller actions is defined by the default route in the Global.asax file. If you want to modify the URL pattern for invoking actions, you canmodify this default route. we will learn more about creating custom routes when we will discuss Routing.
Returning Action Results
A controller action always returns an ActionResult. The ASP.NET MVC framework includes the following types of ActionResults.
  • ViewResult — Represents an ASP.NET MVC view.
  • PartialViewResult — Represents a fragment of an ASP.NET MVC view.
  • RedirectResult — Represents a redirection to another controller action or URL.
  • ContentResult — Represents raw content sent to the browser.
  • JsonResult — Represents a JavaScript Object Notation result (This is useful in Ajax scenarios).
  • FileResult — Represents a file to be downloaded.
  • EmptyResult — Represents no result returned by an action.
  • HttpUnauthorizedResult — Represents an HTTP Unauthorized status code.
  • JavaScriptResult — Represents a JavaScript file.
  • RedirectToRouteResult — Represents a redirection to another controller action or URL using route values.
Typically, you don’t directly return an ActionResult from a controller action. Instead, you call a controller method that returns an ActionResult. For example, if you want to return a ViewResult, you call the controller View() method.
Here’s a list of controller methods that return ActionResults :
  • View( ) — Returns a ViewResult
  • PartialView() — Returns a PartialViewResult
  • RedirectToAction() — Returns a RedirectToRouteResult
  • Redirect() — Returns a RedirectResult
  • Content() — Returns a ContentResult
  • Json( ) — Returns a JsonResult
  • File( ) — Returns a FileResult
  • JavaScript() — Returns a JavaScriptResult
  • RedirectToRoute( ) — Returns a RedirectToRouteResult
we will use these all methods in our programming further….
The goal of this post was to provide depth explanation of how you can create controllers and controller actions. here you were provided with an overview of the different types of ActionResults that can be returned from a controller action.
Enjoyed ?? This was all about Controller and We have tried our best to explain……
Hope this helps….