我怎样才能用PHP端有asp.net的一面呢? [英] How can I have asp.net side by side with php?

查看:220
本文介绍了我怎样才能用PHP端有asp.net的一面呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp.net MVC3网站。它取代了旧的PHP网站。很多人都在引用书签的位置的.php网站的部分,我想补充的回asp.net网站一样简单转发到新的位置。所以mysite的/ product.php会重定向到mysite的/用户映射/ product.cshtml为例。当我插入product.php到该目录中,并使用一个锚点HREF它,提示我有一定的程序中打开或保存。任何想法?

I have an asp.net mvc3 website. It replaced an older php website. Many people have parts of the site bookmarked in reference to the .php locations and I would like to add those back into the asp.net site as simple forwards to the new location. So mysite/product.php would redirect to mysite/usermap/product.cshtml for example. When I insert the product.php into the directory and use an anchor href to it, I am prompted to open it with a certain program or save it. Any ideas?

推荐答案

您可以做一个小的重定向器,并添加匹配类似 mysite的/(编号)的.php

You could make a small redirection controller, and add a route to match something like mysite/{id}.php.

然后在控制器

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


修改

在你的Global.asax.cs文件

In your global.asax.cs file

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 = "" }
    );
}

那么就需要定义一个 OldPathRedirectionController 控制器/ 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();
        }
    }
}

我清理,截至一点点从原来的建议。有希望应该足以让你开始!明显的变化是不难code PHP文件名到MVC控制器的地图,也许改变了路线,让额外参数,如果你需要。

I cleaned that up a little from the original recommendation. That hopefully should be enough to get you started! Obvious changes are to not hardcode the map of php filenames to mvc controllers, and perhaps altering the route to allow extra params if you require that.

这篇关于我怎样才能用PHP端有asp.net的一面呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆