0

ASP.NET を使用して URL を書き直し、コード ビハインドを C# として使用する必要があります。私のアプリケーションには次の URL が含まれています...

http://www.mywebsite.com/Products.aspx?id=1&pg=1

ただし、ユーザーが次の URL を入力したときに、ユーザーが上記の URL と同じコンテンツを取得するように、URL を書き換える必要があります...

http://www.mywebsite.com/CategoryName/ProductName/1

必要な完全なコードを手伝ってくれる人はいますか? つまり、web.config、Global.asax など...

4

2 に答える 2

0

私が提案できるいくつかのオプションがあるかもしれません:

1

の設定を見ることができますHttpHandlerFactory

私は自分で書いた:http://mvcsnippets.blogspot.co.uk/2012/10/custom-cms-using-httphandlerfactory.html

基本的な要点は次のとおりです。

namespace Web.Helpers {
public class HttpCMSHandlerFactory : IHttpHandlerFactory
{
public IHttpHandler GetHandler(HttpContext context, string requestType, string url,string pathTranslated)
{
    string pageName = Path.GetFileNameWithoutExtension(context.Request.PhysicalPath);

    //on Server.Transfer the context is kept, so this just overrides the existing value.
    if (context.Items.Contains("PageName")) 
    {
        context.Items["PageName"] = pageName; } else { context.Items.Add("PageName", pageName); } 
        FileInfo fi = new FileInfo(context.Request.MapPath(context.Request.CurrentExecutionFilePath)); 

        //if File is not physical 
        if (fi.Exists == false) 
        {
             //return page to CMS handler the context containing "PageName" is passed on to this page, which then calls to the database to return the copy.
                return PageParser.GetCompiledPageInstance(url, context.Server.MapPath("~/CMSPage.aspx"),  context);                  
        } 
        else 
        {
            // Returns real page.
            return PageParser.GetCompiledPageInstance(context.Request.CurrentExecutionFilePath, fi.FullName, context); 
        } 
    }
}

私が処理しようとしていた動作は、CMS コンテンツがある可能性があり、情報を提供する必要があるたびにページを作成する必要がなかったが、返されるべき物理ページが存在する場合でした。

あなたにとっては、URL を受け入れて、それを構成要素に分解したいかもしれません。

そうhttp://www.mywebsite.com/CategoryName/ProductName/1なります:

context.Items["Categoryname"] = valueFromUrlasCategoryName; 
context.Items["Productname"] = valueFromUrlasProductName; 
context.Items["Id"] = valueFromUrlasId (or pg); 

return PageParser.GetCompiledPageInstance(url, context.Server.MapPath("~/ControlPage.aspx"),  context);  

次に、制御ページで、context渡された値からこれらの値をインターセプトし、必要に応じてデータを調べることができます。

web.config で、HttpHandlerFactory への参照をポイントします

<httphandlers>
<add path="*.aspx" type="Web.Helpers.HttpCMSHandlerFactory, Web.helpers" verb="*"/>
</httphandlers>

あなたの場合、パスを「.」として設定して、すべてのトラフィックをキャプチャできます。これは、画像とスクリプトの処理を追加する必要があることを意味します。

また、拡張子のないページの IIS にワイルドカードを追加する必要があります。

Web にはHttpHandlerFactoriesに関する記事がたくさんあり、より適切に説明されている可能性があります。

あなたが求めているのは の一部ですがMVC、それを使用するように UI を変更することを検討できますか?

于 2013-03-08T13:18:48.457 に答える