简体   繁体   English

C#URL重写

[英]C# URL Rewriting

I have no knowledge of doing this sort so apologies for my lack of code examples (I've been doing internet searches on the subject and I can't seem to find any examples of what I'm trying to do). 我不知道这样做的原因,因为我缺乏代码示例(我一直在互联网上搜索该主题,而且似乎找不到我正在尝试的示例),对此我深表歉意。

I've got a series of URL's which looks something like these: 我有一系列看起来像这样的URL:

/content/product-A/
/content/product-B/

and I need to write a rule to redirect them to the following: 并且我需要编写一条规则以将其重定向到以下内容:

/result/?product=producta
/result/?product=productb

From what I've read I should be doing this in a rewrite rule in the web.config but other than that I'm really ignorant as to how this would be achieved (if possible). 从我所读的内容中,我应该在web.config中的重写规则中执行此操作,但除此之外,我对如何实现(如果可能)实在一无所知。

Edit from comment: The products will be something like my-widget , my-super-widget , super-gold-widget etc 编辑评论:这些产品将类似于my-widgetmy-super-widgetsuper-gold-widget等。

Could a more intelligent soul give me some assistance please? 请问一个更聪明的灵魂可以给我些帮助吗?

Thanks, C 谢谢,C

In your content controller you probably have something similar to this: content控制器中,您可能会有类似的content

     // eg: /content
    // eg: /content/product-A
    [Route("content/{productName?}")]
    public ActionResult View(string productName)
    {
        if (!String.IsNullOrEmpty(productName))
        {
            return View("ViewProduct", GetProduct(productName));
        }
        return View("AllProducts", GetProducts());
    }

At this point I think you can redirect to a result action 在这一点上,我认为您可以重定向到result操作

            // eg: /content
            // eg: /content/product-A
            [Route("content/{productName?}")]
            public ActionResult View(string productName)
            {
                if (!String.IsNullOrEmpty(productName))
                {
              productName = productName.Replace("-","").ToLower();
              return RedirectToAction("result", new { product= productName  );
                }
                return View("AllProducts", GetProducts());
            }

And you will need any configuration 您将需要任何配置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM