简体   繁体   中英

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:

/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).

Edit from comment: The products will be something like my-widget , my-super-widget , super-gold-widget etc

Could a more intelligent soul give me some assistance please?

Thanks, C

In your content controller you probably have something similar to this:

     // 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

            // 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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