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.
Related
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.
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();
}
}
I am using mvc4 framework and .net framwork 4.5. I need a url like this:
www.examples.com/name (note: 'name' will be change dynamically)
which routes to the same page.
I have tried like this but getting error
My action method is like this:
public ActionResult Userlist(string status)
{
return View();
}
Route Config
routes.MapRoute(
"user",
"{status}",
new { controller = "Home", action = "Userlist" }
);
How can I create a route syntax and redirect this to a controller?
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Add this route config
routes.MapRoute(
name: "Default_Userlist",
url: "{status}",
defaults: new { controller = "Home", action = "Userlist" },
namespaces: new[] { "MyMvcProject.Controllers" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MyMvcProject.Controllers" }
);
}
Don't forget to replace the "MyMvcProject" with the name of your project.
Issue
This is a bad practice because the value of the status could easily cause conflicts with your action methods.
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.
I am trying to show user details at the following url :
www.website.com/users/yasser
where the last entry yasser is the username I have tried a couple of routes but it just does nt work.
My User controller is as shown below.
public class UserController : Controller
{
public ActionResult Index(string username)
{
var model = _service.GetUserDetails(username);
return View(model);
}
}
I have reffered this and couple of other links, but I really could not figure out how it worked.
Can some one help me out on this. Thanks
Edit :
My current route config is below
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Routes executes from top to the bottom:
routes.MapRoute("UserProfile",
"Users/{username}",
new { controller = "User", action = "Index", username = string.Empty }
);
routes.MapRoute("Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional}
);