Everything about Html.Action And Html.RenderAction In ASP.NET MVC

Everything about Html.Action And Html.RenderAction In ASP.NET MVC

CheapASPNETHostingReview.com | Best and cheap ASP.NET MVC 6 hosting. This article introduces both @Html.Action and @Html.RenderAction. These are used to call a partial view in another view by action method. As we have other options such as @Html.Partial and @Html.RenderPartial to call a partial view in other views then why do we use @Html.Action and @Html.RenderAction ? We use these two html helper methods in the following scenarios.

  1. To call partial view in another view.
  2. Partial view is independent to corresponding view in other words partial view model in not related to corresponding view model in strongly typed views.
  3. We need some operations over data of partial view before it render in corresponding view.
  4. We call a partial view from ChildActionOnly action methods in another view by GET request.

Whenever we got above situation in our application then we prefer to use these Html helper methods. Now let’s have a look on summery information of these Html helper methods.

@Html.Action

This Html.Action renders partial view as an HTML string so we can store it in another string variable. It is string return type method so first it returns result as a string then renders result to response.

@Html.RenderAction

This is also same as Html.Action but main difference is that it renders result directly to response that’s why it is more efficient if the action returns a large amount of HTML over @Html.Action.

Now we will have a look on particle implementation of these two.

Using Code

We create an MVC application which has a view for employee login and employee registration as both employee login and employee registration are independent to each other. To combine these on single view, we create two partial views, one for Employee login and another for employee registration. As these two views are also independent to corresponding views in which these call.

web2

First of all we create two view models one for Employee Login (EmployeeLoginViewModel) and another for Employee Registration (EmployeeViewModel). The following code snippet shows both.

As per figure 1, we create an Index view which has both employee login and employee registration partial view so we define a controller that has three action methods. The “Index” action method returns main view and other two child action methods “EmployeeLogin” and “EmployeeRegistration”. The following code snippet shows EmployeeController.

In our example, the Index view is nota strongly typed view. If it is strongly typed then it doesn’t have any impact on partial view model because with help of @Html.Action and @Html.RenderAction, we can use independent models in partial views. Now have a look that how partial views are called in corresponding view. The following code snippet for Employee Login partial view.

Get ASPHostPortal 15% discount with this promotional link

The following code snippet for Employee registration view.

Now we call both partial views with help of @Html.Action and @Html.RenderAction in main index view. The following code snippet is for the same.

Now run the application and we get the same  result as figure 2.

wwwwww

How to Structure The Application in ASP.NET MVC Areas

How to Structure The Application in ASP.NET MVC Areas

CheapASPNETHostingReview.com | Best and cheap ASP.NET MVC hosting. This week I discovered a difficulty related to structuring an ASP.NET MVC web applications one advancement crew was dealing with. The things they have been trying to do was quite straightforward: to create a folder framework each having their particular subfolders for View/Controller/Scripts/CSS and so on. The appliance assets like JS/CSS etc. were not getting rendered properly. The difficulty was owing to NET.config file lying underneath the subfolder, which when moved to the Views folder below that subfolder things went good. The purpose of this post just isn’t to debate regarding the specifics of that difficulty and it is solution.But to discuss regarding how we will very easily framework our ASP.NET MVC Web application as per distinct modules, which is an clear need for just about any big application.

ASP.NET MVC follows the paradigm of “Convention Over Configuration” and default folder structure and naming conventions operates good to get a smaller sized software. But for relatively bigger a single there is a necessity to customise.The framework also offers adequate provisions for the same.You’ll be able to have your personal controller manufacturing facility to get custom methods to making the controller classes and custom see engine for finding the rendering the sights. But when the necessity would be to structure the applying to distinct subfolders as per modules or subsites I believe using “Area” in ASP.NET MVC will likely be useful to make a streamlined software.

You can add a area to some ASP.NET MVC undertaking in Visual Studio as shown beneath.

aspm

as1

Here I have added an area named “Sales”. As shown in the figure below a folder named “Areas” is created with a subfolder “Sales”. Under “Sales” we can see the following

  • The standard folder of Models/Views/Controllers
    • A Web.config under the Views folder. This contains the necessary entries for the RazorViewEngine to function properly
  • A class named SalesAreaRegistration.

res

The code (auto generated) for the SalesAreaRegistration class is shown below:

 System.Web.Mvc.AreaRegistration may be the summary base class use registering the places to the ASP.NET MVC Web Application. The method void RegisterArea(AreaRegistrationContext context) must be overriden to sign up the realm by providing the route mappings. The class System.Web.Mvc.AreaRegistrationContext encapsulates the mandatory information (like Routes) required to sign-up the area.

In Global.asax.cs Application_Start occasion we must RegisterAllAreas() technique as demonstrated under:

 The RegisterAllAreas method looks for all types deriving from AreaRegistration and invokes their RegisterArea method to register the Areas.

Now with the necessary infrastructure code in place I have added a HomeController and Index page for the “Sales” area as shown below.

e

I have to change the Route Registration for the HomeController to avoid conflicts and provide the namespace information as shown below:

 Now I will add a link to the Sales area by modifying the _Layout.cshtml as shown below:

Here I am navigating to the area “Sales” from the main application so I have to provide area information with routeValues. The following overload is being used in the code above:

public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes);

Cheap ASP.NET Hosting Tutorial – Migration From ASP.NET Web API 2 to MVC 6

Cheap ASP.NET Hosting Tutorial – Migration From ASP.NET Web API 2 to MVC 6

CheapASPNETHostingReview.com | If you create a new MVC 6 project from the default starter template, it will contain the following code in the Startup class, under ConfigureServices method:

How to Migrating From ASP.NET Web API 2 to MVC 6

This pretty much explains it all – the Compatibility Shim is included in an external package, Microsoft.AspNet.Mvc.WebApiCompatShim and by default is switched off for new MVC projects. Once added and enabled, you can also have a look at the UseMvc method, under Configure. This is where central Web API routes can be defined:

Inheriting from ApiController

Since the base class for Web API controllers was not Controller but ApiController, the shim introduces a type of the same name into MVC 6.

Limited Edition !! Super Cheap ASP.NET MVC 6 Hosting only $1

While it is obviously not 100% identical to the ApiController from Web API, it contains the majority of public proeprties and methods that you might have gotten used to – the Request property, the User property or a bunch of IHttpActionResult helpers.

Returning HttpResponseMessage

The shim introduces the ability to work with HttpResponseMessage in MVC 6 projects. How is this achieved? First of all, the Microsoft.AspNet.WebApi.Client package is referenced, and that brings in the familiar types – HttpResponseMessage and HttpRequestMessage.

datam

On top of that, an extra formatter is injected into your application – HttpResponseMessageOutputFormatter. This allows you to return HttpResponseMessage from your actions, just like you were used to doing in Web API projects!

How does it work under the hood? Remember, in Web API, returning an instance of HttpResponseMessage bypassed content negotiation and simply forwarded the instance all the way to the hosting layer, which was responsible to convert it to a response that was relevant for a given host.

In the case of MVC 6, the new formatter will grab your HttpResponseMessage and copy its headers and contents onto the Microsoft.AspNet.Http.HttpResponse which is the new abstraction for HTTP response in ASP.NET 5.

As a result such type of an action as the one shown below, is possible in MVC 6, and as a consequence it should be much simpler to migrate your Web API 2 projects.

Binding HttpRequestMessage

In Web API it was possible to bind HttpRequestMessage in your actions. For example this was easily doable:

The shim introduces an HttpRequestMessageModelBinder which allows the same thing to be done under MVC 6. As a result, if you relied on HttpRequestMessage binding in Web API, your code will migrate to MVC 6 fine.

How does it work? The shim will use an intermediary type, HttpRequestMessageFeature, to create an instance of HttpRequestMessage from the ASP.NET 5 HttpContext.

HttpRequestMessage extensions

Since it was very common in the Web API world to use HttpResponseMessage as an action return type, there was a need for a mechanism that allowed easy creation of its instances. This was typically achieved by using the extension methods on the HttpRequestMessage, as they would perform content negotiation for you.

HttpError

If you use/used the CreateErrorResponse method mentioned above, you will end up relying on the HttpError class which is another ghost of the Web API past rejuvenated by the compatibility shim.

HttpError was traditionally used by Web API to serve up error information to the client in a (kind of) standardized way. It contained properties such as ModelState, MessageDetail or StackTrace.

It was used by not just the CreateErrorResponse extension method but also by a bunch of IHttpActionResults – InvalidModelStateResult, ExceptionResult and BadRequestErrorMessageResult. As a result, HttpError is back to facilitate all of these types.

Cheap ASP.NET MVC Hosting Tutorial – Reason Why you Should Start Using ASP.NET MVC

Cheap ASP.NET MVC Hosting Tutorial – Reason Why you Should Start Using ASP.NET MVC

CheapASPNETHostingReview.com | Cheap and Reliable ASP.NET MVC 6 Hosting. For those looking for a reason to use ASP.NET MVC, I have 10 reasons why now is a great time to use ASP.NET MVC.

If you are already a developer of PHP or Java (or even .NET) or you are just interested in learning how to build web applications, this is probably the best time to get into ASP.NET MVC which is why I’m going to give you my top 10 reasons for using ASP.NET MVC.

Here is why you should look into ASP.NET MVC.

<strong>No System.Web</strong>

For those us WebForm users, this was always a large assembly that was always required to make a web application. If you decide to move towards the ASP.NET MVC Core 1.0, this brand new framework removes the Elvis-like, hunky System.Web library (3MB assembly footprint), making it faster to load an ASP.NET web site and appeasing the Google Monster.

<strong>A Brand New Sleeker 2016 Model</strong>

ASP.NET MVC 5 is dead…Love Live ASP.NET MVC 5. Recently, .NET was completely rewritten and renamed (ASP.NET Core 1.0). This makes a developer’s life even easier. Instead of the development cycle of writing code, compiling, and testing the functionality, they eliminated the middle man (compiling). This makes the development process faster and it provides quick feedback to developers in Visual Studio.

 

<strong>Feels More Natural</strong>

Everyone who knows me knows that I’ve been a Microsoft developer since the Classic ASP days and when I moved to WebForms, it didn’t feel like a natural transition. When MVC came along, building web applications felt more natural and the workflow seemed to fit.

<strong>It's One Of The Popular Kids</strong>

Since ASP.NET runs on IIS and it’s one of the most popular web servers on the web, this makes ASP.NET MVC a natural fit. Most corporations started with IIS back in the day with Classic ASP.

<strong>Easier JavaScript Integrations</strong>

With WebForms, things can get a little sticky with JavaScript. I’ve seen developers hard-coded JavaScript in a Page_Load in WebForm’s code-behind…a lot! ASP.NET MVC allows for easier JavaScript framework integration. Try to put AngularJs into a WebForms web app and tell me you don’t have any hair left by the end of the day.

<strong>Open Source</strong>

Anyone can now see the code. If you want to expand on the View and create your own ViewEngine, examine the code at Github to find out how they did it. Source code galore to make your imagination run wild!

<strong>Cross-Platform</strong>

Yes, it’s true. You can now code web applications using an Apple, Linux, or Windows platform.

 

<strong>We Ain't In Visual Studio Any More</strong>

Since the mere mention of a newer .NET/ASP.NET release, various vendors (including Microsoft themselves) started building different IDEs to show off the capabilities of the newly-reformed .NET. Such IDEs like Visual Studio Code and JetBrains’ new C# IDE called Rider will allow you to build ASP.NET and C# projects without using a fully-loaded Visual Studio. Now, it doesn’t matter what you use to code your Microsoft web solutions

<strong>Healthy Community</strong>

ASP.NET MVC was a breath of fresh air for most Microsoft web developers. It took us into a new web era and moved us away from WebForms

<strong>Unit Testable</strong>

Out of the box, ASP.NET MVC is unit testable. Making your code unit-testable provides a certain checks-and-balances. To some WebForm developers, this was a new concept. It was difficult to test some UI and business components. With ASP.NET MVC, you can easily test all of your components and have a strong piece of mind that everything works as coded when it ships.

Conclusion

Since this was a quick post about where and why to start using ASP.NET MVC, I thought I could provide some additional material regarding where to go from here.

If you are interested more about ASP.NET MVC 6 hosting, I would recommend you to host with ASPHostPortal.com.

Untitled

Cheap ASP.NET MVC 6 Hosting – How To Improve Your Website’s SEO In 3 Easy Steps

Cheap ASP.NET MVC 6 Hosting – How To Improve Your Website’s SEO In 3 Easy Steps

CheapASPNETHostingReview.com | Best and Cheap ASP.NET MVC 6 hosting review and comparison. Did you know that the default route of ASP.NET MVC will produce multiple URL’s that display the same content? And as I am sure you are aware, duplicate content can cause all sorts of SEO problems. In this post, I will show you three steps to prevent duplicate content with ASP.NET MVC 6.

1. Use attribute routing instead of the default route

Attribute Routing is a new feature added to ASP.NET MVC 6 that allows you to manually define each route by placing an attribute on each action.

Why use attribute routing?

Create a new ASP.NET MVC 6 application, and you will find the following URL’s all show the home page:

  • http://localhost
  • http://localhost/home
  • http://localhost/home/

Now I know, that it is unlikely that the search engine will discover the duplicate URL’s, but it is better to make sure only one can be accessed, and this can be done by using attribute routing.

Also, in my experience I have found that using attribute routing instead of the default route makes it easier to debug routing issues.

How to enable attribute routing?

  1. Remove the default route from your RouteConfig. Or even better, replace the default route with a catch all that points to an action that displays 404 not found.
  2. Enable Attribute routing by calling the routes.MapMvcAttributeRoutes() method in your RouteConfig class.
  3. Manually add the route to each action. For example, assigning [Route("")] to the index action of the home controller will fix the issue above.

2. Add or remove the trailing forward slash from your URL’s

There is still a slight issue. For example, with the following attribute applied to the about page:

The about page can be accessed using the following URL’s:

  • http://localhost/about
  • http://localhost/about/

You can fix this by adding a rewrite rule to your Web.config file. For example, the following rule will remove the trailing forward slash from all URL’s:

Just remember to set the AppendTrailingSlash to false in the RouteConfig class so that the routing system generates URL’s that have no trailing forward slash.

3. Redirect none www to www

It’s best practice to have either the www redirect to the none www or the www redirect to the www. For example, on this site cheapaspnethostingreview.com redirects to www.cheapaspnethostingreview.com This prevents duplicate content.

The following example shows a rewrite rule that redirect the none www to the www subdomain.

The other good thing about the rule above, is it will redirect all domains to the primary domain. For example, if you host your site on ASPHostPortal. The ASPHostPortal.com domain will be redirected to your main domain, preventing more duplicate content.