controller's action parameter doesn't accept / - c#

I have controller's action
public string Index(string id)
{
return id;
}
I have only this route in Global.asax.cs
routes.MapRoute(
"Default",
"{id}",
new { controller = "Start", action = "Index", id = UrlParameter.Optional }
);
For url like this "http://localhost/stuff" and "http://localhost/hello" it works. But it doesn't work for url like "http://localhost/stuff/add". How can I fix it?

Add wildcard (asterisk) before the id:
routes.MapRoute(
"Default",
"{*id}",
new {controller = "Start", action = "Index", id = UrlParameter.Optional}
);

Related

Take string input on default index method in mvc5

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

How to hide controller and action name from start url of my mvc project

This Is my route config file:-
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
RouteValueDictionary keyValues = new RouteValueDictionary();
PDCL.API.API _api = new PDCL.API.API();
string cities = _api.GetCities();
string Specialities = _api.GetRouteSpecialization();
routes.MapRoute("SearchClinicListing", "{cityname}/{clinic}/{doctortype}/{clinicname}", new { controller = "Listing", action = "ClinicSearch" }, new RouteValueDictionary { { "cityname", cities } });
routes.MapRoute("AreaListing", "{cityname}/{clinicname}/{areaname}", new { controller = "Listing", action = "AreaSearch" }, new RouteValueDictionary { { "cityname", cities }, { "clinicname", Specialities } });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Listing", action = "Index", id = UrlParameter.Optional }
);
when I run my project, Default url is http://localhost:2315/listing/index
how can I hide listing/index from that url ?
Did you tried moving your default route above SearchClinicListing, Let me know if it works
In order to have:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Listing", action = "Index", id = UrlParameter.Optional }
Why don't you have it?
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
Rename your controller from ListingController to HomeController since you want it as your starting controller and than when you lunch you project you will have: http://localhost:2315
Removing the controller and action name from URL is not a good idea. But if you want to do this you should map new route before your Default route:
routes.MapRoute(
name: "MyCustomRoute",
url: "{id}",
defaults: new { controller = "Listing", action = "Index", id = UrlParameter.Optional }
);

.net set landing pages from start

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}

Why do ASP.NET MVC URLs need Index

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

Working with routes.maproute and parameters

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

Categories