link to another controller with hot towel and dura

2019-05-12 07:54发布

问题:

I am developing an mvc4 app with multiple spa's.

I am using the hot towel template for my spa.

What I want to do is have a anchor link within my views for a spa go to the index action for another controller, so my users can exit one spa and open another. Not all actions will start a spa some are regular mvc style pages.

I have put code like like the following in my spa html pages:

<a href="/controller/action">Navigate to another controller</a>

This will change the url in the browser but always reloads the default html page for the spa. If I hit the refresh button in the browser then if will go to the proper page.

I have been able to put a target on the anchor tag of _parent or _top like:

 <a href="/controller/action" target="_parent">Navigate to another controller</a>

and it will navigate to the new controller page.

I believe it is something in the durandal framework that is preventing the spa from navigating to the second controller, but since I am just starting to work with this, I am stumped as what I need to change. I think there should be a better way than using the target in the anchor tag, or is that the best option.

回答1:

I hope that I have understood you correctly. I believe this behaviour is caused my Sammy.js (which the durandal router is currently based on). The default behaviour of Sammy is to hijack all links and process them as regards to the SPA itself.

I didn't like this behaviour for the website I am working on and needed links to other MVC web pages to be possible from within the SPA. What I really wanted is for regular links to just work and hash links to be interpreted as a link to another view within the SPA. So '/controller/action' would go to another web page and '#moduleId' would go to another durandal V/VM.

I found this Sammy option that works for me and set it in my main.js

    // This stops Sammy from hijacking regular links
    Sammy.Application.prototype.disable_push_state = true;