Windows 2012 Hosting - MVC 6 and SQL 2014 BLOG

Tutorial and Articles about Windows Hosting, SQL Hosting, MVC Hosting, and Silverlight Hosting

ASP.NET Core Hosting :: Which is Better? ASP.NET Core Razor Pages or MVC?

clock March 4, 2019 10:50 by author Jervis

With the release of new ASP.NET Core 2 framework, Microsoft and its community has provided us with a brand new alternative for the MVC (Model-View-Controller) approach. Microsoft has named it Razor Pages, and while it’s a little bit different approach, but it’s still similar to MVC in some ways.

In this article, we are going to cover following important points of ASP.NET Razor Pages.

  • Razor Pages — what is it exactly?
  • Drawbacks of Using ASP.NET MVC
  • Advantages of Using Razor Pages
  • A Quick Comparison of How Requests Are Handled in Both

Razor Pages — What is It Exactly?

A Razor Page is very similar toASP.NET MVC’s view component. It has basically same syntax and functionality as MVC.

The key difference between Razor pages and MVC is that the model and controller code is also included within the Razor Page itself.

In simple terms, it is much like am MVVM (Model-View-View-Model) framework. It provides two-way data binding and a simpler development experience with isolated concerns.

Though MVC works fine with web apps that have large amount of dynamic server views, single page apps, REST APIs, and AJAX calls, but Razor Pages are perfect for simple pages that are read-only or do basic data input.

Now, the ASP.NET MVC has been extremely popular for web applications development, and it definitely has its benefits. In fact, the ASP.NET WebForms was specifically designed as an MVVM solution in MVC.

But, the new ASP.NET Core Razor Pages is the next evolution of ASP.NET WebForms.

Drawbacks of ASP.NET MVC

As most of you probably know, MVC stands for Model-View-Controller. It is an architectural pattern used in software development for implementing UI (user interfaces).

While MVC is one of the most popular framework and is being used by millions of web developers worldwide, but it still has its drawbacks. Let’s look at the two most important of them.

#1 — Complexity

In ASP.NET MVC, there are piles of concepts such as TempData, RouteCollection, ViewData, Linq to SQL, Controller Action, Lambda Expression, Custom Route, and HTML Helpers, all of which tie the Model, View, and Controller.

Now, you cannot build a web application using ASP.NET MVC until you learn all these basic concepts. Plus, even if you’ve learned them, you will still face complexity issues at times, especially when you’re building large-scale applications.

#2 — Cost of Frequent Updates

In ASP.NET MVC, web developers cannot completely ignore the view of the model even when both are separated. The reason is because when the model is changed frequently, the views of your application could be flooded with update requests.

Views are basically graphical displays which takes some time to render depending on the complexity of your application. And if your application is complex and the model has been changed a lot, then the view may fall behind update requests. So, the developers then need to spend extra time fixing this situation, resulting into higher costs.

Advantages of Using Razor Pages

We’ve been providing ASP.NET MVC development services for about 10 years now. In fact, we’re certified Microsoft Gold Partner. So, based on our knowledge, experience, and expertise, there are two main benefits of using ASP.NET Core razor pages instead of MVC.

#1 — Razor Pages is Better Organized

If you’ve ever used MVC for any kind of web development, then you probably know that how much time it takes to code an entire app. Creating dynamic routes, naming things properly, and hundred other stuff consumes a lot of time.

Razor Pages, on the other hand, is more organized compared to MVC.

In Razor Pages, the files are basically more organized. You have a Razor View and the entire code behind a file, same way the old ASP.NET WebForms did.

#2 — Single Responsibility

Again, if you have ever used an MVC framework before, you have probably seen that there are some huge controller classes which are generally filled with a lot of various actions. These classes are like a virus which grows bigger and bigger as new things are added.

But, in Razor Pages, each app page is self-contained with its own view and code organized together, which as a results, is less complex than the MVC.

Overall, ASP.NET Core is a Modular Web Framework.

In MVC, if you add new things, then the .NET framework will force you to release a new version.

For example, Microsoft released routing in MVC 4, and later, they released Attribute routing for which they again had to release another new framework MVC 5.

In ASP.NET Core, on the other hand, everything is managed using the NuGet package, which means it is easier than MVC to upgrade existing framework without releasing new .net framework version every time new things are added.

Additionally, In .NET Core, any community can release an update for new NuGet package version, and you can receive the latest changes by just updating your NuGet packages.

A Quick Comparison of How Requests Are Handled in Both

We explained in above points that building a web application using ASP.NET Core Razor Pages is less complex than the MVC. Here, we will demonstrate that with action.

Let’s start with MVC

Here’s a quick overview of how MVC handles the requests.

 

As you can see, routing is the key to how MVC decides to handle requests. The default configuration for routing is the combination of action and controller names.

So if you request for /staff/index, then it will route you the action named Index on the StaffController class.

But, it can be customized or configured to route any request to any controller with a block of code.

Now Compare the Same with Razor Pages

Here’s a quick overview of how Razor Pages handle the requests.

The difference between the two is that in Razor Pages, when you make a request, the default routing configuration will find a Razor Page for that specific request in the Pages folder.

Suppose you make a request for /contact/, then ASP.NET Core will look for a page having same name that you used in request and will route you directly to it.

That means, a request to /contact/ will route you to Contact.cshtml

And for any .cshtml file to be considered as a Razor Page, it must be placed in the Pages folder and also contain @Page in its markup.

This way, the Razor Page will then act as a controller action. Now comparing this to MVC, the configuring the custom route will be less complex as there will no extra coding involved.

In Conclusion

Razor Pages seem like a promising start for modern web app development with less complexity in 2018. And as the comparison has shown that it provides the benefit of containing everything related to a particular request at one place, where in MVC requires spreading pieces all around your app like a giant puzzle which you will then have to put back together with extra efforts of coding.



ASP.NET Hosting - ASPHostPortal.com :: How to Bind Data to a Label From a SqlDataSource

clock April 25, 2016 21:24 by author Dan

Label controls in ASP.NET don't have a smart tag that allows you to select a data source, so at first glance, it is not easy to see how to bind a value returned from a SqlDataSource or AccessDataSource control to the label. Here's how to programmatically access the data returned by such a DataSource and apply it to a non-databound control.

To programmatically access the contents of a SqlDataSource or AccessDataSource control you need to explicitly call its Select() method. This method accepts a single input parameter of type DataSourceSelectArguments. This parameter can contain information regarding the filters to apply or the column to Order By. For example, when working with a sortable GridView, sorting a column calls the Select() method, and passes in a DataSourceSelectArguments instance with its SortExpression property set to the column name the user chose to sort by. If you don't want the DataSource to sort or filter, you pass in DataSourceSelectArguments.Empty.

Depending on the DataSourceMode of the DataSource control, one of two objects are returned when the Select() method is called. If the DataSourceMode is set to DataReader, a DataReader object is returned. The type of DataReader (SqlDataReader, OleDbDataReader, OdbcDataReader etc) that is returned depends entirely on the provider type used - in other words, whether you are using the OleDb provider, SqlClient provider etc. It has nothing to do with the type of DataSource control. The examples below both query an Access database, but one uses the AccessDataSource control, and the other uses the SqlDataSource control. Both return OleDbDataReaders, because it is the OleDbProvider library that is used for the connection.

If the DataSourceMode is set to Dataset, or not set at all (which means that the default setting of Dataset is used) the object that is returned is a DataView. A DataView is like a DataTable on steroids. It exposes methods that allow you to filter and sort data, for example, and bind it. A DataView contains a collection of DataRowView objects, which represent each row in the returned results.

So, with a DataReader, you would access the values during the DataReader.Read() operation, in very much the same way as if you are using plain ADO.NET code, whereas with the DataSet, you would need to create an object of the appropriate type - DataView, then iterate the DataRowView collection to access the values. In this, the code is remarkably similar to accessing values directly from a DataSet's table collection using plain ADO.NET.

The code below shows the contents of an aspx file, which contains two label controls, and two SqlDataSource controls. Each SqlDataSource control has its DataSource mode set to alternative values - DataSet and DataReader, and both of them have an OnSelecting event defined in which the value of the EmployeeID parameter is assigned:

<asp:Label ID="Label1" runat="server" /> <asp:Label ID="Label2" runat="server" />

<asp:SqlDataSource
    ID="SqlDataSource1"
    runat="server"
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
    DatasourceMode="DataSet"
    SelectCommand="SELECT [LastName], [FirstName] FROM [Employees] WHERE ([EmployeeID] = ?)"
    OnSelecting="SqlDataSource1_Selecting">
    <SelectParameters>
        <asp:Parameter Name="EmployeeID" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>

<asp:SqlDataSource
    ID="SqlDataSource2"
    runat="server"
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
    DatasourceMode="DataReader"
    SelectCommand="SELECT [LastName], [FirstName] FROM [Employees] WHERE ([EmployeeID] = ?)"
    OnSelecting="SqlDataSource2_Selecting">
    <SelectParameters>
        <asp:Parameter Name="EmployeeID" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>


The following code snippet shows the aspx.cs file contents, where the parameter values are set in the Selecting event handler. In the Page_Load method, the data returned by each of the Sql DataSource controls is accessed and a value consigned to a label. The method of access depends on the DataSource mode, but is identical for both SqlDataSource and AccessDataSource:

[C#]
protected void Page_Load(object sender, EventArgs e)
{

    DataView dvSql = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
    foreach (DataRowView drvSql in dvSql)
    {
        Label1.Text = drvSql["FirstName"].ToString();
    }

    OleDbDataReader rdrSql = (OleDbDataReader)SqlDataSource2.Select(DataSourceSelectArguments.Empty);
    while (rdrSql.Read())
    {
        Label2.Text = rdrSql["LastName"].ToString();

    }
    rdrSql.Close();
}



protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
    e.Command.Parameters["EmployeeID"].Value = 2;
}

protected void SqlDataSource2_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
    e.Command.Parameters["EmployeeID"].Value = 2;
}

[VB]
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

Dim dvSql As DataView =
    DirectCast(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)
    For Each drvSql As DataRowView In dvSql
        Label1.Text = drvSql("FirstName").ToString()
    Next

Dim rdrSql As OleDbDataReader =
    DirectCast(SqlDataSource2.Select(DataSourceSelectArguments.Empty), OleDbDataReader)
    While rdrSql.Read()

        Label2.Text = rdrSql("LastName").ToString()
    End While
    rdrSql.Close()
End Sub

Protected Sub SqlDataSource1_Selecting(ByVal sender As Object,
    ByVal e As SqlDataSourceSelectingEventArgs)
    e.Command.Parameters("EmployeeID").Value = 2
End Sub

Protected Sub SqlDataSource2_Selecting(ByVal sender As Object,
    ByVal e As SqlDataSourceSelectingEventArgs)
    e.Command.Parameters("EmployeeID").Value = 2
End Sub


When using this technique with Sql Server - or more specifically the SqlClient provider, change OleDbDataReader to SqlDataReader in the above code. Happy Coding.

Best Windows Shared Hosting Recommendation

ASPHostPortal.com provides our customers with Plesk Panel, one of the most popular and stable control panels for Windows hosting, as free. You could also see the latest .NET framework, a crazy amount of functionality as well as Large disk space, bandwidth, MSSQL databases and more. All those give people the convenience to build up a powerful site in Our Shared Hosting. We offers Windows hosting starts from $5/month only. We also guarantees 30 days money back and guarantee 99.9% uptime. If you need a reliable affordable Windows Shared Hosting, we should be your best choice.



About ASPHostPortal.com

We’re a company that works differently to most. Value is what we output and help our customers achieve, not how much money we put in the bank. It’s not because we are altruistic. It’s based on an even simpler principle. "Do good things, and good things will come to you".

Success for us is something that is continually experienced, not something that is reached. For us it is all about the experience – more than the journey. Life is a continual experience. We see the Internet as being an incredible amplifier to the experience of life for all of us. It can help humanity come together to explode in knowledge exploration and discussion. It is continual enlightenment of new ideas, experiences, and passions

 photo ahp banner aspnet-01_zps87l92lcl.png

Author Link

Corporate Address (Location)

ASPHostPortal
170 W 56th Street, Suite 121
New York, NY 10019
United States

Sign in