carlosherrera
carlosherrera

Reputation: 187

MVC 4 HomeController constructor and action always fired

I've been looking for a solution for 2 days now. Maybe I'm mind blocked so I have to ask for help.

I have a project in MVC 4 which has many controllers, models and views. I kept the HomeController and used the Index action as the default entry for the site. There I have a grid, something like this:

<div class="ui-grid-b home-icon-grid">
    <div class="ui-block-a"><a href="@Url.Action("Index", "Cart")"><div class="home-icon" style="background-color:#b13a3a"><i class="fa fa-3x fa-shopping-cart"></div></a><div class="icon-title">@Mobile.Resources.Resources.resCart</div></div>                 
    <div class="ui-block-c"><a href="@Url.Action("Category", "Products", new {id = "ROOT"})"><div class="home-icon" style="background-color:#1c70ef"><i class="fa fa-3x fa-tags"></i></div></a><div class="icon-title">@Mobile.Resources.Resources.resItems</div></div>            
    <div class="ui-block-c"><a href="@Url.Action("Index", "Settings")"><div class="home-icon" style="background-color:#6b6b6b"><i class="fa fa-3x fa-gear"></i></div></a><div class="icon-title">@Mobile.Resources.Resources.resSettings</div></div>  
  </div> 

As expected, every time I start debugging, I can see the method

public ActionResult Index() {code}

in HomeController.cs is fired.

Also, in Fiddler I see the following at the beggining:

Host: localhost:55883
URL: /

(Sorry I don't post images, I don't have enough reputation yet)

So far so good. Now, when I click on one of the links in the grid, let's say "Items". I expect to see the Products list, and I do, but if I'm debugging, I see that the Index method in the HomeController is fired as well. An then the Category method in ProductsController is called.

And not only that. Let's say I'm seeing the Products list, and I click in some other link (which go to other view, I've tested many cases) the HomeController Index() is called too.

In Fiddler, when I click the Items link, I see first "/" in the URL column, and then the "Products/Category/ROOT" one.

Following the example, in that view, I clicked over a link which go to "Search/Result", in Fiddler I see "/" and then "Search/Result?productCategory=..."

This happens in all over the site. Every time I click on an anchor, Home/Index is called first, and then the selected path.

I even tried using a simple anchor:

<a href="http://google.com">Google</a>

And again, first the Home/Index, and then it redirected to Google.

I suppose it could be related to Route Configuration, or maybe there's something with POSTs being made. Maybe is the normal flow in the mvc life cycle and I'm freaking out over nothing, hehe.

Any ideas? Thanks in advance. I hope I was clear enough.

Upvotes: 0

Views: 273

Answers (1)

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93444

Many modern browsers "preload" linked pages. So when you load a page, it may also load pages that are linked to those pages. So if you have links back to your home page, then those pages might be loaded by the browser to improve performance.

Upvotes: 1

Related Questions