I am have a parameter that I want to use as the default ending of a URL, but it is currently a parameter. I want it to always direct to the Home Controller & Index Action, but the "/" becomes the pageName parameter.
For example, I want
http://localhost:18792/?pageName=HomePage
To be accessible by:
http://localhost:18792/HomePage
This is my current routes.maproute:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{pageName}",
defaults: new { controller = "Home", action = "Index", pageName = "HomePage" }
);
To do this, you will have to create a route for each page. Don't modify the "Default" route. . Add a new route:
routes.MapRoute(
name: "HomePage",
url: "HomePage",
defaults: new { controller = "Home", action = "HomePage" }
);
for dynamic routes, you could use:
routes.MapRoute(
name: "PageName",
url: "{pageName}",
defaults: new { controller = "Home", action = "Index", pageName = "defaultPage" }
);
which will map to ~/defaultpage or ~/homepage with the Action "Index" in the "Home" controller:
public ActionResult Index(string pageName)
{
ViewBag.Message = pageName;
return View();
}
Related
i want to take input of username value on default mvc5 home page controller Index method. I already tried to do it like bellow [Route("{Username?}")] but it suppose to get value on url: https://localhost:44330/myusername but this url not hits the Index method. Can i do it without changing in RouteConfig.cs or how can i do it changing RouteConfig.cs even?
public class HomeController : Controller
{
[Route("{Username?}")]
public ActionResult Index(string Username)
{
return View();
}
}
RouteConfig.cs code:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
You can easily do it from RouteConfig.cs. Example codes are bellow:
routes.MapRoute(
name: "Default",
url: "{username}",
defaults: new { controller = "Home", action = "Index", username = UrlParameter.Optional },
namespaces: new[] { "ProjectNameSpace.Controllers" }
);
I have defined my routes in .NET app like this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Register",
url: "Register",
defaults: new { controller = "Home", action = "Register" }
);
routes.MapRoute(
name: "Login",
url: "Login",
defaults: new { controller = "Home", action = "Login" }
);
routes.MapRoute(
name: "ResetPwd",
url: "{action}/{id}",
defaults: new { controller = "Home", action = "ResetPwd", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
So that I can have routes like following:
mysite.com/Login
mysite.com/Register
mysite.com/ResetPwd
Now the problem arises with other controller when I try to redirect the user to dashboard when hes logged in successfully from mysite.com/Login like this:
return RedirectToAction("Index", "Dashboard");
However I'm getting an error:
The resource cannot be found.
I presume it want's me to define a route now for dashboard/index... but isn't that nuts since then I'd have to add every route manually ???
What am I doing wrong here?
Change your resetpassword route to
routes.MapRoute(
name: "ResetPwd",
url: "ResetPwd/{id}",
defaults: new { controller = "Home", action = "ResetPwd", id = UrlParameter.Optional }
);
Currently your provided url /Dashboard/Indexis looked up by this route with Dashboard being an Action on Default Home controller which doesn't exists in my opinion.
I have two different MapRoutes in Route.config as follows...
routes.MapRoute(
name: "Default",
url: "{action}",
defaults: new { controller = "Index",action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "SubjectRoute",
url: "{action}",
defaults: new { controller = "Subjects", action = "Subjects" }
);
and my #HTML.ActionLink is as follows in Index action method which is in IndexController
#Html.ActionLink("SUBJECTS", "Subjects","Subjects", new { }, new { #class = "text" })
Now when I click on SUBJECTS link in Index action method, it should go to Subjects action in Subjects controller with out displaying controller name in the URL.
How can this be done?
routes.MapRoute(
name: "SubjectRoute",
url: "Subjects",
defaults: new { controller = "Subjects", action = "Subjects" }
);
routes.MapRoute(
name: "Default",
url: "{action}/{id}",
defaults: new { controller = "Index",action = "Index", id = UrlParameter.Optional }
);
With the above, calls to
#Html.ActionLink("SUBJECTS", "Subjects","Subjects", new { }, new { #class = "text" })
should generate
/Subjects
The default and more general route will now catch all other requests and route them to the IndexControllerwhich now acts as the site root/index.
for example assuming
public class IndexController : Controller {
public ActionResult Index() {...}
public ActionResult ContactUs() {...}
public ActionResult About() {...}
}
the available routes based on above controller are
/
/Index
/ContactUs
/About
I have page, which content and URL changes from database. However, I want to set URL like
localhost:xxxx/MyPage
Now I got
localhost:xxxx/Pages/MyPage
which I don't need. What can I do for change it?
My route
routes.MapRoute(
name: "LandingPage",
url: "Pages/{*urltitle}",
defaults: new { controller = "Admin", action = "LandPage", urltitle = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
My code for change content and URL
public ActionResult LandPage()
{
string temp = RouteData.Values["urltitle"].ToString();
var item = RepositoryManager.Instanse.LandingContentRepository.GetItemByURL(temp);
IEnumerable<LandingContent> list = new List<LandingContent>() { item };
ViewBag.HtmlStr = item.HTMLText;
return View(ViewBag);
}
Well, I kinda solved it
my final route
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "LandPage",
url: "{segment}",
defaults: new { controller = "Home", action = "LandingPage" }
And in HomeController you do this
[AllowAnonymous]
public ActionResult LandingPage(string segment)
{some logic}
Struggling to work out how to have the following
http://localhost/star/regulus
the only way it will work is to have a web address of
http://localhost/star/index/regulus
This is my routes
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute( "", "star/{sstar}", new { Controller = "star", id = UrlParameter.Optional });
and in the Controller, I set up the View as :-
return View("~/views/star.cshtml", oStar);
so you can see I'm not using Index.cshtml
Any ideas how I get the top web address, not the second one.
You need to rearrange your routes
routes.MapRoute(
name: "StarRoute",
url: "star/{sstar}",
defaults: new { controller = "Star", action = "Star", sstar = UrlParameter.Optional}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The default controller should usually be the last route registered
Assuming controller
public class StarController :Controller {
public ActionResult Star(string sstar){
//...other code that uses sstar parameter
return View();
}
}
The view should be located at...
~/Views/Star/Star.cshtml
...for the following URIs to work:
http://localhost/star/regulus
http://localhost/star?sstar=regulus
http://localhost/star/sirius
http://localhost/star?sstar=sirius