适用于 IIS 和 Javascript 的 MVC 路由

发布于 2024-09-30 15:04:25 字数 1339 浏览 2 评论 0原文

我认为我有一个相当容易解决的问题,但我似乎无法让它按照我的需要工作。通过 Visual Studio 运行 Web 应用程序时,我得到一个 URL,例如: http://localhost:50261/Controller/Action 但是当我将其部署到 IIS 时,URL 设置如下: http://devServer/AppName/Controller/Action

我的 ajax URL 请求无法正确路由,遇到问题。在本地运行 Web 应用程序时,我的 ajax URL 是: /Controller/Action 但当我部署到开发 IIS 时,我必须将其更改为: /AppName/Controller/Action以便正确路由 ajax 请求。

因此,我一直在尝试创建正确的 mapRoute,将我的 ajax URL 保留为 /AppName/Controller/Action,这样我就不必在每次部署时都进行调整。这就是我现在所拥有的,但不起作用:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Survey", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

            routes.MapRoute(
                "Dev", // Route name
                "AppName/{controller}/{action}/{id}", // URL with parameters
                new { controller = "DefaultController", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

使用这些路由,如果我尝试 URL:http://localhost:50261/AppName/ 它找不到任何东西。我有点困惑我做错了什么?谢谢!

I think I have a fairly easy issue to fix but I can't seem to get it to work as I need. When running the web app through Visual Studio I get a URL such as: http://localhost:50261/Controller/Action But when I deploy it to IIS the URL is setup like so: http://devServer/AppName/Controller/Action.

I'm having an issue with my ajax URL requests not getting routed correctly. When running the web app locally my ajax URL is: /Controller/Action but when I deploy to the dev IIS I have to change it to: /AppName/Controller/Action for the ajax request to get routed correctly.

So I've been trying to create the correct mapRoute to leave my ajax URL to /AppName/Controller/Action so I don't have to adjust it each time I deploy. This is what I have right now thats not working:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Survey", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

            routes.MapRoute(
                "Dev", // Route name
                "AppName/{controller}/{action}/{id}", // URL with parameters
                new { controller = "DefaultController", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

With these routes if I try the URL: http://localhost:50261/AppName/ it can't find anything. I'm a little confused at what I'm doing wrong? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故人的歌 2024-10-07 15:04:25

我想我应该再等一会儿,直到在 SO 上发布我的问题,我想通了。我本来打算删除这个问题,但是,如果其他人遇到这个问题,我想我会留下它。

我的问题与 MVC 路由无关,更多地与 Visual Studio 开发服务器的设置有关。我所要做的就是正确放入“虚拟路径”以匹配我的 IIS 结构,现在 Web 应用程序可以在本地运行,并且在部署时无需进行任何更改。

I guess I should have waiting a little longer until posting my question on SO, I figured it out. I was going to delete the question but, in case someone else runs across this I thought I'd leave it.

My issue had nothing to do with MVC Routing, but more with the Visual Studio Development Server's settings. All I had to do is correctly put in the "Virtual path" to match my IIS structure and now the web app works locally and when deployed without making changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文