Routing issue in application using MVC/Durandal and located in IIS subfolder

余生颓废 提交于 2019-12-23 02:24:21

问题


I´m looking for a solution for my problem I mentioned in the title. First of all you need to know that I have two MVC applications published on an IIS. The first application is kind of a central management system so I set up a Site called "Management" in IIS. The second app is a MVC web shop embedded as application (subfolder) in Site "Management".

For the shop I created an MVC application using durandal.

Now it seems like I have a routing problem. When I enter the URL defined in Site "Management" I see the central management, which is completely right. The URL looks like:

http://mySite.com/

When I try to visit my shop I just go to the following URL:

http://mySite.com/Shop

This application only contains one .cshtml file which renderes the different pages of the shop using Durandal. This also works, but when I enter my credentials, Durandal seems to join the routing because it adds an "#/" to the URL. What happens then is that I´m redirected to the HomeController index view of my parent application (http://mySite.com). Thats what I don't want!

Can anyone of you help me in this issue?

I need to add that I don't have any problems locally when I run the shop so I guess the subfolder thing and/or durandal is the cause of this behavior.

Thanks in advance!

Yheeky


回答1:


Yep, it's the subfolder "Shop" that's causing this behavior. I had the exact same problem with the applications I was developing. Here's the solution I used. I feel like it's sort of a hack, but it does the job.

First, after digging into the RequireJS config documentation, I figured out how to pass on variables to a module in the main.js file.

requirejs.config({
  paths: {
    ...
  },
  config: {
    'modules/routes': {
      subfolder: 'Shop'
    }
  }
});

I then created a routes.js module under an /App/modules/ folder in the project whose sole responsibility it to construct URLs for the rest of my application. Within that module, I wrote a function that determines whether or not the script is running locally or on a web server. Based on that, it then uses the subfolder variable I passed it in the RequireJS config to construct the root URL the application will use for all routing.

define(['module'],
  function (module) {
    // if the application is being hosted on http://localhost, do not include the application name
    var domain = window.location.protocol + '//' + window.location.host,
        root = (window.location.hostname !== 'localhost') ? domain + '/' + module.config().subfolder : domain;

    function getUrl(controller, action) {
      return (action) ? root + '/' + controller + '/' + action : root + '/api/' + controller;
    }

    return {
      getUrl: getUrl,
      cart: getUrl('Home', 'Cart'),
      ...
    }
});

Note that it's important that you pass in 'module' in order to read the variable from the RequireJS config.



来源:https://stackoverflow.com/questions/19542748/routing-issue-in-application-using-mvc-durandal-and-located-in-iis-subfolder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!