Abhishek Mathur
Abhishek Mathur

Reputation: 316

URL rewriting issue through Route.Config

I am able to do the URL rewriting of my MVC Application by using a Route.Config file as below:

    //Offline Consult Route
    routes.MapRoute(
        name: "WrittenStep2",
        url: "written/step2/{id}",
        defaults: new { controller = "Offline", action = "Step2", id = UrlParameter.Optional }
        );

    routes.MapRoute(
        name: "Written",
        url: "written",
        defaults: new { controller = "Offline", action = "Index" }
        );

In the above code, I have Controller as "Offline" and have some actions.

I am able to change the route:

TO: www.abc.com/written

FROM www.abc.com/Offline

My problem is that I am still able to access the URL: www.abc.com/Offline. How can I resolve this issue?

I have tried to deny access of this URL to the users by using the Begin_Request method of Global.asax file.

But after doing that I won't be able to access my methods which I am calling using jQuery Ajax.

$.ajax({
    type: "POST",
    url: "/Offline/HelloWorld",
    data: jsonString,
    contentType: "application/json",
    ...

Is there any way to restrict users from using the same URL?

Upvotes: 0

Views: 1033

Answers (1)

ITevfik
ITevfik

Reputation: 184

Try this, it might be helpful for you ... for the given example, if you want to restrict or navigate users for a specific URL, you can try:

1- include a route for Offline with custom route handler

            routes.MapRoute(
              "OfflineWithoutParameterRoute",
              "Offline"
             ).RouteHandler = new NewUrlRouteHandler();

        routes.MapRoute(
            name: "WrittenStep2",
            url: "written/step2/{id}",
            defaults: new { controller = "Offline", action = "Step2", id = UrlParameter.Optional }
            );

        routes.MapRoute(
            name: "Written",
            url: "written",
            defaults: new { controller = "Offline", action = "Index" }
            );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

2- Create your MvcRouteHandler as

    public class NewUrlRouteHandler : System.Web.Mvc.MvcRouteHandler
{
    protected override IHttpHandler GetHttpHandler(System.Web.Routing.RequestContext requestContext)
    {
        //restrict or navigate to any destination
        requestContext.RouteData.Values["controller"] = "Home";
        requestContext.RouteData.Values["action"] = "index";

        return base.GetHttpHandler(requestContext);
    }
}

which you can maintain any approach

so any hit for www.abc.com/Offline will hit to custom handler and any route for www.abc.com/Offline/prm will follow the default route.

I have tried with controller/action you given as an example and it should be helpful for you.

    public class OfflineController : Controller
{
    // GET: Offline
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Step2(int id)
    {

        return View();
    }
    public ActionResult HelloWorld(int id)
    {

        return View();
    }
}

Upvotes: 1

Related Questions