【问题标题】:How can I have asp.net side by side with php?我怎样才能让 asp.net 与 php 并排?
【发布时间】:2012-01-03 06:02:13
【问题描述】:

我有一个 asp.net mvc3 网站。它取代了旧的 php 网站。许多人将站点的某些部分标记为引用 .php 位置,我想将它们添加回 asp.net 站点,作为对新位置的简单转发。例如,mysite/product.php 将重定向到 mysite/usermap/product.cshtml。当我将 product.php 插入目录并对其使用锚点 href 时,系统会提示我用某个程序打开它或保存它。有什么想法吗?

【问题讨论】:

    标签: php asp.net-mvc-3


    【解决方案1】:

    您可以制作一个小型重定向控制器,并添加一个路由来匹配 mysite/{id}.php 之类的内容。

    然后在那个控制器中

    public ActionResult Index(string id)
    {
        return RedirectToActionPermanent("Product", "YourExistingController", id);
    }
    

    编辑

    在您的 global.asax.cs 文件中

    public void RegisterRoutes(RouteCollection routes)
    {
        // you likely already have this line
         routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
        // assuming you have a route like this for your existing controllers.
        // I prefixed this route with "mysite/usermap" because you use that in your example in the question
        routes.MapRoute(
            "Default",
            "mysite/usermap/{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    
        // route to match old urls
        routes.MapRoute(
            "OldUrls",
            "mysite/{oldpath}.php",
            new { Controller = "OldPathRedirection", action = "PerformRedirection", oldpath = "" }
        );
    }
    

    然后你会定义一个OldPathRedirectionControllerControllers/OldPathRedirectionController.cs 最有可能)

    public class OldPathRedirectionController : Controller
    {
        // probably shouldn't just have this hard coded here for production use.
        // maps product.php -> ProductController, someotherfile.php -> HomeController.
        private Dictionary<string, string> controllerMap = new Dictionary<string, string>()
        {
            { "product", "Product" },
            { "someotherfile", "Home" }
        };
    
        // This will just call the Index action on the found controller.
        public ActionResult PerformRedirection(string oldpath)
        {
            if (!string.IsNullOrEmpty(oldpath) && controllerMap.ContainsKey(oldpath))
            {
                return RedirectToActionPermanent("Index", controllerMap[oldpath]);
            }
            else
            {
                // this is an error state. oldpath wasn't in our map of old php files to new controllers
                return HttpNotFoundResult();
            }
        }
    }
    

    我从最初的建议中稍微整理了一下。希望这足以让你开始!明显的变化是不将 php 文件名的映射硬编码到 mvc 控制器,并且如果需要,可能会更改路由以允许额外的参数。

    【讨论】:

    • 虽然这不是我的想法,但它看起来更像是手动插入 .php 文件的最佳实践。你能扩大一点吗?特别是路线需要什么定义?
    • 非常感谢rejj!我非常感谢您为此付出的努力。
    【解决方案2】:

    如果您使用的是 IIS7,Url 重写模块非常棒。这是页面:http://www.iis.net/download/urlrewrite

    当我将product.php插入目录并使用锚href指向它时,系统会提示我用某个程序打开它或保存它。有什么想法吗?

    手动更新处理程序映射。但是我很确定当您安装 PHP for IIS (http://php.iis.net/) 时,它会为您完成。

    【讨论】:

    • 是的,我的开发服务器没有安装 php,所以这就是问题所在,谢谢。但是,生产服务器有它,所以这个问题已经解决了。
    • 那么 Url 重写呢?很多人都成功使用过。
    • 很遗憾,我没有足够的权限访问生产服务器上的 IIS,无法安装 Url rewrite。它看起来像是一个有用的工具,但我将不得不使用基于代码或文件系统的实现。
    • 一旦由管理员(或其他)安装,您的 Url 重写可以在应用程序的 web.config 级别定义。
    【解决方案3】:

    使用此站点将 PHP 安装到 IIS 中。 http://php.iis.net/

    【讨论】:

    • 这代表了我的问题。我正在与部署环境不同的环境中进行开发,并且没有 PHP 支持。在部署时它对我来说很好,不过谢谢你的提示。
    • 然后将其安装在您的开发环境中。
    • .php 将没有功能,否则我会。只要它在生产服务器上工作就可以了。感谢您指出显而易见的事情,因为我有时会想念它 - 如果没有您的建议,我不会检查生产服务器。
    猜你喜欢
    • 1970-01-01
    • 2021-05-27
    • 2017-09-07
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多