Where should the Home Page logic be located in MVC?

I am experimenting with MVC, and my question is , where did I have the Page_Load logic on the main pages with WebForms, where should it go in MVC? Here's a business example:

  • Different host headers should cause different pages to be displayed on the main page of the site (one), so all pages. For example, if the host title is hello.mydomain.com, the page title should be “Hello World” for all pages / views, and goodbye.mydomain.com should be “Goodbye” for all pages / views.
  • If the host header is different from everything I have on the list, no matter where in the application, it should redirect to / Error / NoHostHeader.

I used to put this in the MasterPage Load () event, and it looks like MVC, I could do it either in each controller (it does not have the right to call this function on each controller), or somewhere in Global.asax (it seems, too ... global?).

Edit: I got this to work successfully using the Global.asax method in combination with a controller for actually processing the data. The only problem at this stage is that all host header information is in the database. Usually I save the information of the "tenant" if you are in the Session variable, and only make a DB call when it does not exist; Is there a better way to do this?

+3
4

MVC 1:1 MVC, , MVC:

: " , ( , - ). , ".

: " ".

: " ".

, , -. , "", , .

: " MVC" -, - ASP.NET MVC!

( ), HttpModule, . BeginRequest, - HttpContext.Current.Items [ "" ]. (, , .)

( - ) HttpContext, :

public string Tenant
{
    get { return HttpContext.Current.Items["tenant"]; }
}

:

  • ( ) ( ),
  • , , , .
  • , , CSS, ..
  • , , , , cookie, , , , , .

re edit. , cookie , . , -. , , , , .

+9

, ViewData MVC Master View, . ActionExecuting, , .

+3

, "WebForms" MVC. - , html. , , . : , WebForms, .

, , Global.asax... , : P

http://forums.asp.net/t/1226272.aspx

protected void Application_BeginRequest(object sender, EventArgs e)
        {
            string host = string.Empty;

            if (this.Request.ServerVariables["HTTP_HOST"] == this.Request.Url.DnsSafeHost)
            {
                host = this.Request.Url.DnsSafeHost;
            }
            else
            {
                Regex regex = new Regex("http://[^/]*.host/([^/]*)(/.*)");
                Match match = regex.Match(this.Request.Url.AbsoluteUri);

                if (match.Success)
                {
                    host = match.Groups[1].Value;
                    Context.RewritePath(match.Groups[2].Value);
                }
            }

            // Match the host with the portal in the database
            ...
        } 
+3

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


All Articles