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.
Related
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 }
);
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 what to create a url that looks like website.com/sea/season/23/team
Here is the MapRoute
routes.MapRoute(
"SeaTeam",
"sea/season/{seasonId}/team/{action}/{name}",
new { controller = "Team", action = "Index", name = UrlParameter.Optional }
);
The Html.ActionLink looks like
#Html.ActionLink("Add Team", "Index", "SeaTeam", new { seasonId = seasons.id }, null)
But its generating the following url
Add Team
Any insights? Thanks...
Update
Here is the controller
public class TeamController : Controller
{
//
// GET: /Team/
public ActionResult Index()
{
//get the seasons of the loged user
var loadedTeam = tempFunctions.getSeasonsOf(CustomHelper.UserGuid(User.Identity.Name));
return View("Team/ManageTeam", loadedTeam);
}
try:
#Html.ActionLink(
"Add Team", // linkText
"Index", // Action
"Team", // Controller
new { seasonId = seasons.id },
null)
I don't see a SeaTeam Controller
My route looks like:
routes.Add(new Route("{companyName}/{action}/{id}", new MvcRouteHandler())
{
Defaults = new RouteValueDictionary(new { controller = "CompanyController", action = "Index", id = 1 }),
}
);
my action:
public ActionResult Index(string companyName, string id)
{
Response.Write(companyName);
Response.End();
return ViePage("~/views/company/index.aspx");
}
try this:
routes.Add(new Route("{companyName}/{action}/{id}", new MvcRouteHandler())
{
Defaults = new RouteValueDictionary(new { controller = "Company", action = "Index", id = 1 }),
}
);
when referencing your controllers you don't want to have the "controller" part of the name there.
I'm at a loss... here's my route:
routes.MapRoute("LangOnly", "{language}",
new { controller = "Home", action = "Root", language = "en" },
new { language = #"en|ja" });
it matches www.domain.com/en, but does not match www.domain.com/ja.
huh? I've even gone so far as to comment out any other routes... kind of stuck. ;/
Update: Here's the root action on the Home controller.
[CompressFilter]
public ActionResult Root()
{
if (!IsEnglish)
return RedirectToAction("Index", "Biz", new { b = "" });
return Request.IsAuthenticated ? View("LoggedInRoot") : View("Root");
}
It doesn't take a language parameter because it's being set on the base controller in OnActionExecuting, like so:
var l = (RouteData.Values["language"] != null) ? RouteData.Values["language"].ToString() : string.Empty;
if (string.IsNullOrEmpty(l))
l = "en";
if (l.Contains("en"))
{
IsEnglish = true;
l = "en";
}
else
{
IsEnglish = false;
l = "ja";
}
ViewData["lang"] = l.ToLower();
Language = l.ToLower();
Works perfectly for me with your route. Try this simple configuration:
routes.MapRoute("LangOnly", "{language}",
new {controller = "Home", action = "Index", language = "en"},
new {language = #"en|ja"});
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
And your action:
public ActionResult Index(string language)
{
.....
(I am using "Index" as the action here, obviously change it to "Root" if that is in fact your action name.)