I want to accept /User/ and /User/213123
Where 213123 is a parameter (user_id)
Here is my RouteConfig.cs:
routes.MapRoute(
name: "user",
url: "{controller}/{action}/{username}",
defaults: new { controller = "User", action = "Index", username = UrlParameter.Optional }
);
And my UserController.cs:
public ActionResult Index()
{
ViewData["Message"] = "user index";
return View();
}
[Route("user/{username}")]
public ActionResult Index(string username)
{
ViewData["Message"] = "!" + username + "!";
return View();
}
This works in .net-core 1.0 but not in mvc5. What am I missing?
Thank you
EDIT:
Having just this in my UserController.cs also doesn't work (returns 404):
public ActionResult Index(string username)
{
if (!String.IsNullOrEmpty(username))
{
ViewData["Message"] = "Hello " + username;
}
else
{
ViewData["Message"] = "user index";
}
return View();
}
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /user/asd
EDIT2:
Updated RouteConfig.cs:
routes.MapRoute(
"userParam", "user/{username}",
new { controller = "user", action = "IndexByUsername" },
new { username = #"\w+" }
);
routes.MapRoute(
name: "user",
url: "user",
defaults: new { controller = "User", action = "Index"}
);
/User/ now calls IndexByUsername with Index as the username
/User/asd still returns 404
EDIT4: Current code:
RouteConfig.cs:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute("userParam", "user/{username}", new { controller = "user", action = "Index"});
routes.MapRoute("user", "{controller}/{action}/{username}", new { controller = "User", action = "Index", username = UrlParameter.Optional });
UserController.cs:
public class UserController : Controller
{
public ActionResult Index(string username)
{
if (!String.IsNullOrEmpty(username))
{
ViewData["Message"] = "Hello " + username;
}
else
{
ViewData["Message"] = "user index";
}
return View();
}
}
You need only one action method with the signature
public ActionResult Index(string username)
and in that method you can check if the value of username is null or not.
Then you route definitiosn needs to be (note the user route needs to be placed before the default route)
routes.MapRoute(
name: "user",
url: "user/{username}",
defaults: new { controller = "User", action = "Index", username = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Related
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.
I have three url types. These:
first: http://localhost/
second: http://localhost/X/
third: http://localhost/X/Y/
Examples Url:
http://localhost/test/
http://localhost/test/details/
first:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
}
);
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
return View();
}
}
second:
routes.MapRoute(
"Module",
"{module_name}/{controller}/{action}",
new
{
controller = "Module",
action = "Index",
module_name = UrlParameter.Optional
}
);
public class ModuleController : Controller
{
//
// GET: /Module/
public ActionResult Index(string modul_name)
{
return View();
}
}
third:
routes.MapRoute(
"ModuleDetails",
"{module_name}/{details_param}/{controller}/{action}",
new
{
controller = "ModuleDetails",
action = "Index",
module_name = UrlParameter.Optional,
details_param = UrlParameter.Optional
}
);
public class ModuleDetailsController : Controller
{
//
// GET: /ModuleDetails/
public ActionResult Index(string modul_name, string details_param)
{
return View();
}
}
in this instance;
http://localhost/X/
response: "Home", "Index"
but;
http://localhost/X/
response: Application in the server error. Resource Not Found.
http://localhost/X/Y/
response: Application in the server error. Resource Not Found.
How can I do?
Thanks, best regards..
http://localhost/X/
response: Application in the server error. Resource Not Found.
This happens because each of your routes specifies at least 2 mandatory parameters.
Try to add this one:
routes.MapRoute(
"Default",
"{controller}/{action}",
new
{
controller = "Home",
action = "Index"
}
);
that's right friends..
routes.MapRoute(
"Default", // Route name
"", // URL with parameters
new { controller = "Home", action = "Index" } // Parameter defaults
);
routes.MapRoute(
"Module",
"{modul_name}",
new { controller = "Modul", action = "Index", modul_name = UrlParameter.Optional }
);
routes.MapRoute(
"Page",
"{modul_name}/{page_name}",
new { controller = "Page", action = "Index", modul_name = UrlParameter.Optional, page_name = UrlParameter.Optional }
);
I have created one custom route
routes.MapRoute("Exams",
"Exams/{id}/{name}",
new { controller = "Exam", action = "SingleExam", id = "", name = (string) null },
new[] { "MockTests.FrontEnd.Controllers" }
);
For this I am creating link as
http://localhost:61575/Exams/12/maharashtra+mba+common+entrance+test
But his gives error as
The resource cannot be found
SingleExam code
public ActionResult SingleExam(int id,string name)
{
return View();
}
Other Routes
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "User", action = "Register", id = UrlParameter.Optional },
new[] { "MockTests.FrontEnd.Controllers" }
);
What wrong I am doing ?
I have a problem, my url is ugly smth like this:
http://localhost:43547/Admin?id=1&str=wooh
I want see the url like this one
http://localhost:43547/LOL
My routes are:
routes.MapRoute(
"MyRoute",
"LOL",
new { controller = "Admin", action = "Index",
id = UrlParameter.Optional, str = UrlParameter.Optional }
);
routes.MapRoute(
"Default",
"{controller}/{action}",
new { controller = "Home", action = "Index" }
);
and I have 2 identical views one of them is with such method
#Html.ActionLink("Click me", "Index", "Admin", new { id = 1, str = "wooh" }, null)
which moves the page...
and I have 2 controllers:
public ActionResult Index(int id = 5485, string str = "Default Value")
{
ViewBag.ID = id;
ViewBag.STR = str;
return View();
}
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Controller = "Home";
ViewBag.Action = "Index";
return View();
}
}
So what's wrong? When I type smth like /LOL
It moves me to another page with default values of id and str...
Change your MyRoute definition to include the id and str parameter values.
routes.MapRoute(
"MyRoute",
"LOL",
new { controller = "Admin", action = "Index",
id = 1, str = "wooh" }
);
That way your url will look like /LOL but will pass the values you need.
I have a Controller named About and an Action named Index. I want the URL to be like this (action name will be dynamically):
www.example.com/about/aaa
www.example.com/about/bbb
www.example.com/about/ccc
Routing
routes.MapRoute(
name: "About",
url: "{controller}/{name}",
defaults: new { controller = "About", action = "Index"}
Controller
public class AboutController : Controller
{
// GET: /About/
public ActionResult Index(string name)
{
return View();
}
}
View
#{
ViewBag.Title = "Index";
}
<h2>Index About</h2>
This should work.
routes.MapRoute(
name: "About",
url: "About/{name}",
defaults: new
{
controller = "About",
action = "Index"
});
Make sure your default route exists and comes after About route
routes.MapRoute(
name: "About",
url: "about/{name}/{id}",
defaults: new { controller = "About", action = "Index", id=UrlParameter.Optional}
You can pass an ActionResult name as a parameter:
public ActionResult Index(string name)
{
return View(name);
}
public ActionResult First()
{
return View();
}
public ActionResult Second()
{
return View();
}
In the View:
#Html.ActionLink("Get Action named Firts" "Index", "Home", new {name = "First"}, null)