0

I have tried to implement slug url like this

www.abcd.com/xyz/er/productname

where "/xyx/er" is area and it's working, But when ever I am trying to go to another page like

www.abcd.com/xyz/er/Login

then it's just keep redirecting.

Here is my code for RegisterArea:

 context.MapRoute(
                name: "product",
                url: "xyz/er/{*slug}",
                defaults: new { controller = "er", action = "Index", slug = UrlParameter.Optional }
                 );

context.MapRoute(
                "default",
                "xyz/{controller}/{action}/{id}",
                new { controller="er", action = "Index", id = UrlParameter.Optional }

            );

and my controller code is:

public ActionResult Index(string slug=null)
{
    if (slug != null && (slug != "Index" && slug != "index"))
    {
        //it will show the product
    }
    else
    {
        //redirect to another page
    }
}
  • here slug get replaced by whatever you type after `er/` (other then index) and redirect to `er/index` and //it will show the product –  Jan 10 '18 at 07:05
  • Because `www.abcd.com/xyz/er/Login` hits you first route definition and goes to the `Index()` method which will try to display a product named `Login` –  Jan 10 '18 at 07:07
  • I am not sure if this work, try change your url into `url: "xyz/er/Index/{*slug}"` and your url will look like `www.abcd.com/xyz/er/Index/productname`. and in other case `www.abcd.com/xyz/er/Login`. –  Jan 10 '18 at 07:12
  • i have tried with url: "xyz/er/Index/{*slug}" it's working but I don't want this type of url, I want url: "xyx/er/{*slug}". @ashik – Abhisek Bhattacharjee Jan 10 '18 at 07:39
  • If that's the case then you need to create specific routes for all the other methods (e.g. `url "xyz/erLogin"` and locate them before the `product` route –  Jan 10 '18 at 07:52
  • 1
    Or you would need a route constraint that checks if `slug` is a product name, and if not return false so that it falls through to the new route (refer [this answer](https://stackoverflow.com/questions/37358416/routing-in-asp-net-mvc-showing-username-in-url/37359345#37359345) for an example) –  Jan 10 '18 at 07:54
  • Thanks. I have tried previously with constraint but not this way. @StephenMuecke – Abhisek Bhattacharjee Jan 10 '18 at 09:16

1 Answers1

0

with the help of Stephen's comment, I implemented a constraint for checking my product name. If Product name is valid it will show the intended page and if not found then it will redirect to Login page.

The code is given below:

    context.MapRoute(
                    name: "product",
                    url: "xyz/er/{*slug}",
                    defaults: new { controller = "er", action = "Index", slug = UrlParameter.Optional },
 constraints: new { productname= new ProductNameConstraint() }
                     );


public class ProductNameConstraint: IRouteConstraint
        {
            public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
            {
                //logic here
            }
        }