簡體   English   中英

我怎樣才能使asp.net與php並存?

[英]How can I have asp.net side by side with php?

我有一個asp.net mvc3網站。 它取代了舊的php網站。 許多人在網站的某些部分添加了指向.php位置的書簽,我希望將這些內容重新添加到asp.net站點中,以簡單地轉發到新位置。 因此,例如,mysite / product.php將重定向到mysite / usermap / product.cshtml。 當我將product.php插入目錄並使用錨定href時,系統會提示我使用某個程序打開它或保存它。 有任何想法嗎?

您可以制作一個小的重定向控制器,並添加一條路由來匹配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 = "" }
    );
}

然后,您將定義一個OldPathRedirectionController (很有可能是Controllers/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控制器,並且可能更改路由以允許額外的參數(如果需要)。

如果您使用的是IIS7,則URL重寫模塊很棒。 這是頁面: http : //www.iis.net/download/urlrewrite

當我將product.php插入目錄並使用錨定href時,系統會提示我使用某個程序打開它或保存它。 有任何想法嗎?

手動更新處理程序映射。 但是我很確定,當您為IIS安裝PHP(http://php.iis.net/)時,它將為您完成。

使用此站點將PHP安裝到IIS中。 http://php.iis.net/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM