URL rewriting issue through Route.Config - asp.net-mvc

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?

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();
}
}

Related

Change url in Route Config in Mvc

I want to change the address of a function (via RegisterRoutes).
I defined two routes.MapRoute but not working any.
I checked many examples and matched my code with them, but the problem still remains.
The real address is:
http://localhost:3127/account/register/3
I want my address to be changed to the following address:
http://localhost:3127/Reg
Should [Route("Reg")] be used at the top of the function for this?
Is a redirect required for this or not?
Do I need web.config settings to do this?
my action is:
[AllowAnonymous]
[Route("Reg")]
public virtual ActionResult Register()
{
return View();
}
my configuration in Register Routes:
routes.MapRoute(
name: "Register",
url: "Reg",
defaults: new { controller = "Account", action = "Register" },
namespaces: new[] { "WebSite.Controllers" }
);
routes.MapRoute(
name: "Account",
url: "Account/Reg",
defaults: new { controller = "Account", action = "Register" },
namespaces: new[] { "WebSite.Controllers" });
public class RegisterController : Controller
{
public ActionResult Account()
{
return View();
}
}
routes.MapRoute(
name: "Register",
url: "Reg/{id}",
defaults: new { controller = "Register", action = "Account", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I resolve this problem. I install IIS and change my code to below code. Then my address became OK.
routes.MapRoute(
name: "Reg",
url: "Reg",
defaults: new { controller = "Account", action = "Register" },
namespaces: new[] { "WebSite.Controllers" }
);
[Route("Reg")]
public virtual ActionResult Register()
{
return View();
}
Needless to say, I put this piece of code at the beginning of the file RouteConfig
before all cases and after routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
Now, my url is http://localhost:3127/Reg everywhere.

Change url asp.net mvc 5

routes.MapRoute(
name: "MyRoute",
url: "{Product}/{name}-{id}",
defaults: new { controller = "Home", action = "Product", name = UrlParameter.Optional , id = UrlParameter.Optional }
);
my routemap and i want my url in product action be like = http://localhost:13804/Wares/Product/name-id
but now is like =
http://localhost:13804/Wares/Product/4?name=name
When defining a route pattern the token { and } are used to indicate a parameter of the action method. Since you do not have a parameter called Product in your action method, there is no point in having {Product} in the route template.
Since your want url like yourSiteName/Ware/Product/name-id where name and id are dynamic parameter values, you should add the static part (/Ware/Product/) to the route template.
This should work.
routes.MapRoute(
name: "MyRoute",
url: "Ware/Product/{name}-{id}",
defaults: new { controller = "Ware", action = "Product",
name = UrlParameter.Optional, id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Assuming your Product action method accepts these two params
public class WareController : Controller
{
public ActionResult Product(string name, int id)
{
return Content("received name : " + name +",id:"+ id);
}
}
You can generate the urls with the above pattern using the Html.ActionLink helper now
#Html.ActionLink("test", "Product", "Ware", new { id = 55, name = "some" }, null)
I know its late but you can use built-in Attribute Routing in MVC5. Hope it helps someone else. You don't need to use
routes.MapRoute(
name: "MyRoute",
url: "{Product}/{name}-{id}",
defaults: new { controller = "Home", action = "Product", name = UrlParameter.Optional , id = UrlParameter.Optional }
);
Instead you can use the method below.
First enable attribute routing in RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
}
Then in WaresController
[Route("Wares/Product/{name}/{id}")]
public ActionResult Product(string name,int id)
{
return View();
}
Then to navigate write code like this in View.cshtml file
Navigate
After following above steps your URL will look like
http://localhost:13804/Wares/Product/productname/5

Asp.net Routes with two routes

I have two route with same signature but only the parameter names are different how to fix this issue.
Following is my code
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "DefaultRoute2",
url: "{controller}/{action}/{formSubmissionId}",
defaults: new { controller = "Employee", action = "Index", formSubmissionId = "formSubmissionId" }
);
The routes are meant to accept different URL structures. Both of your routes have same structure, so the first one will always match, and the second will never be tested.
Instead of using a different route, in /Employee/Index you should just use the parameter id.
public class EmployeeController : Controller
{
public ActionResult Index(string id)
{
string formSubmissionId = id;
}
}
The URL for that action would be the same that (I believe) you wanted to achieve with the second route: Employee/Index/id
UPDATE
I've just realized. If you only need the parameter formSubmissionId for the action /Employee/Index you could do this:
// Note the order of the routes:
routes.MapRoute(
name: "DefaultRoute2",
url: "Employee/Index/{formSubmissionId}",
defaults: new { controller = "Employee", action = "Index", formSubmissionId = "formSubmissionId" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
public class EmployeeController : Controller
{
public ActionResult Index(string formSubmissionId)
{
// ...
}
}
Now i fix this issue as
routes.MapRoute(
name: "DefaultActivity",
url: "{controller}/LoadActivity/{formSubmissionId}",
defaults: new { controller = "{controller}", action = "LoadActivity", formSubmissionId = UrlParameter.Optional }
);
this is work around of my scenario.

Optional id for default action

I got a site with only this Route:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("Default", "{controller}/{action}/{id}",
new { controller = "Image", action = "Image", id = UrlParameter.Optional }
);
}
This is the controller:
public class ImageController : Controller
{
public ActionResult Image(int? id)
{
if (id == null)
{
// Do something
return View(model);
}
else
{
// Do something else
return View(model);
}
}
}
Now this is the default action so i can access it without an ID just by directly going to my domain. For calling the id it works just fine by going to /Image/Image/ID. However what i want is calling this without Image/Image (so /ID). This doesn't work now.
Is this a limitation of the default Route or is there a way to get this to work?
Thanks
Create a new route specific for this url:
routes.MapRoute(
name: "Image Details",
url: "Image/{id}",
defaults: new { controller = "Image", action = "Image" },
constraints: new { id = #"\d+" });
Make sure you register the above route before this one:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
Otherwise it will not work, since the default route will take precedence.
Here I'm stating that if the url contains "/Image/1" then the ImageController/Image action method is executed.
public ActionResult Image(int id) { //..... // }
The constraint means that the {id} parameter must be a number (based on the regular expression \d+), so there's no need for a nullable int, unless you do want a nullable int, in that case remove the constraint.

can not map a route for a specific controller in mvc 4

I have a controller named Registration and an action method in it as the following:
public JsonResult GetReqs(GridSettings gridSettings, int rt)
{
...//whatever
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
So I added a route and now my RouteConfig.cs is like this:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "RegReq",
url: "{Registration}/{GetReqs}/{rt}",
defaults: new { controller = "Registration", action = "GetReqs", rt = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
However I can't access the rt parameter and the action method GetReqs isn't called(I set a breakpoint on it but nothing happened). Where is the mistake?
Edit: Link example I tried : ~/Registration/GetReqs/1
I think you need to remove the brackets in your first route:
routes.MapRoute(
name: "RegReq",
url: "Registration/GetReqs/{rt}",
defaults: new { controller = "Registration", action = "GetReqs",
rt = UrlParameter.Optional }
);
The default route has {controller} to tell MVC to use the string in that section of the url as the controller name. You know the controller, so you just need to match the specific string.

Resources