Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement View addressing in ASP.NET MVC

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to achieve view addressing in ASP.NET MVC. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

First, why use views?

There are two advantages to using views:

1. Ensure the separation of the page content output and the controller code. Unlike CodeBehind, CodeBehind implements physical separation, and views can be logically separated.

2. A more convenient and intuitive way to output page content. The logic on the page can be easily completed with the help of a mixture of Razor syntax and HTML, unlike the previous WebFrom which requires the back end to integrate all the HTML for output, so it is difficult to combine the background and the front end.

2. View addressing 1. View addressing method 1

When you use the View method to return a view, how do you know which view page to execute? The usual practice is to pass the full path of the view through the view method, or specify the name of the view, which is easy to understand! As shown in the following code:

Public ActionResult Index () {return View ();}

By default, the Index view opens when you start, and the code is modified as follows:

Public ActionResult Index () {/ / specify the path to the view return View ("~ / Views/Home/About.cshtml");}

Running result:

As you can see from the run results, if the path to the view is specified, then the view with the specified path will be opened at run time.

So how do you know where to put the view when you specify the view name (the default code is the method used to specify the view name)? Do you want to traverse the path of the entire site? Of course not, as shown in the following figure, it has an addressing range.

To specify a view name to find a view:

1. First look for the view folder corresponding to the corresponding Controller name, and if you can't find it, look for the Shared folder.

2. Look for the cshtml file first.

3. This is an agreed rule.

2. View addressing method 2

Third, write the view

We have come into contact with the writing of the view, in principle, the processing of the request by ASP.NET is ultimately reflected in the execution of the target Action method, and the request is responded to the result of the processing, while ActionResult provides us with a shortcut to respond to the request. ASP.NET MVC defines a series of native ActionResult types, such as EmptyResult, ContentResult, etc., which either help us to respond to the specified content to the client according to the corresponding media type, or to reply to a response with a specified status code, or to achieve client redirection.

ViewResult is an important and most commonly used ActionResult, and we can use it to render the specified View to the client's browser. View rendering for ViewResult is ultimately implemented using the View engine. View in the View engine implements the IView interface, which corresponds to a certain View file, while the core component ViewEngine implements the acquisition, activation, presentation and final release of View.

The WebViewPage resulting from the final compilation of the View file is a subclass of System.Web.Mvc.WebViewPage, which inherits from WebPageBase. The properties of the three helper objects (HtmlHelper, UrlHelper, and AjaxHelper) that we use when defining View are defined here, and they are initialized by the InitHelpers method.

When we define the View, we can get the state data set in the Controller through the read-only properties Model, ViewData, ViewBag and TempData, or we can represent the current context of the View by calling the SetViewData setting ViewData,ViewContext. As shown in the following figure:

4. HTML auxiliary method

Some common HTML assist methods are listed in the following figure:

This is the end of this article on "how to achieve View addressing in ASP.NET MVC". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report