Placement of a function in an ASP.NET MVC application

I am working on a C # / ASP.NET MVC application for school. This is a social bookmarking service (similar to reddit), and under each "bookmark" in the list I would like to show the base domain of the source URL (as opposed to the full URL). already found some threads on SO on how to do this, so I went ahead and made a class to distract the functionality, but I'm not sure where I should call the method.

Right now, I have a BookmarkList controller method that passes a list of Bookmark objects to a view where I repeat the list. Since the Bookmark object does not have the property to store the base URL (as I compute it on the fly), I cannot put it in Bookmark objects before passing them to the view (and it seems to be anyway). So should I call the GetDomainFromUrl method, which I made from the view itself? For some reason this is not suitable either.

I am not sure how to fit into this functionality without breaking the MVC convention.

Thanks.

+4
source share
3 answers

I would add it to the Bookmark class. Properties can be calculated, here is an example from the asp.net mvc tutorial from msdn ( MSDN source ):

public string LastName { get; set; } public string FirstMidName { get; set; } public string FullName { get { return LastName + ", " + FirstMidName; } } 

Instead of simple concatenating strings, you can call your GetDomainFromUrl method here.

IE

 public string BaseUrl { get { return GetDomainFromUrl(this.Url); } } 

Note that the set method is not defined, since you could / could set the Url property.

Also, is your url stored as a string?

If you use a class

+4
source

For very simple scenarios or when you have full control over your models, MVC (Model, View, Controller) is a good template.

In my experience, you usually need additional information that is important for your views, but not for your real model. For example, a list of drop-down elements that will be displayed for a model property, or in your case, to place the base URL for the site for your users.

In this case, I like to adapt MVC to VM-VC ( ViewModel , View, Controller).

Essentially, you would like to create a ViewModel bookmark and use it when rendering your views:

BookmarkViewModel.cs:

 public class BookmarkViewModel { public string BaseUrl {get;set;} // + all existing bookmark properties } 

You can either add your base URL directly to your view model, or create a view model yourself, or you can do this in your controller when creating the view model.

+1
source

There are several different options for how you could do this. I recommend storing the GetDomainFromUrl () method in a bookmark class. I also recommend creating a property for BaseUrl.

You can then pass the full URL to the Bookmark object in the constructor, execute your function, and set it to the BaseUrl property.

 class Bookmark { public string BaseUrl { get; } public Bookmark(string url) { BaseUrl = GetDomainFromUrl(url); } private string GetDomainFromUrl(string url) { //your logic to generate BaseUrl } } 

Another option is to do something like the following:

 class Bookmark { private string baseUrl; public string BaseUrl { get { return baseUrl; } set { baseUrl = GetDomainFromUrl(value)); } } private string GetDomainFromUrl(string url) { //your logic to generate BaseUrl } } 

and then set the BaseUrl somplace property in your code to the value of the full URL, and when you do this, it will execute your function and save it in your property.

0
source

Source: https://habr.com/ru/post/1433921/


All Articles