Set Default Route for a Controller as Index View - asp.net-mvc

So I have the url:
www.mywebsite/signup/index
That works fine.
What I want to do is add a new route mapping to the RouteConfig class that allows the following url to work:
www.mywebsite/signup
I have tried:
routes.MapRoute(
name: "Signup",
url: "{controller}/",
defaults: new { controller = "Signup", action = "Index", id = UrlParameter.Optional }
And:
routes.MapRoute(
name: "Signup",
url: "{controller}",
defaults: new { controller = "Signup", action = "Index", id = UrlParameter.Optional }
And:
routes.MapRoute(
name: "Signup",
url: "{controller}/*",
defaults: new { controller = "Signup", action = "Index", id = UrlParameter.Optional }
My RouteConfig class has been customized and the default routing when creating a new ASP.NET MVC Project has been changed, it currently looks like this:
routes.MapRoute(
name: "DefaultConstrained",
url: "{controller}/{action}/{id}",
defaults: new { action = "Index", controller = "Home", id = UrlParameter.Optional },
constraints: new { controller = ConstrollersAsCSV() }
);
routes.MapRoute(
name: "SiteLogin",
url: "{site}",
defaults: new { controller = "User", action = "Login", site = "" },
constraints: new { site = new ExcludeAndUseRegex() }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "User", action = "Login", id = UrlParameter.Optional }
);
Where the constraint method ConstrollersAsCSV (which I don't know what that is doing) is:
public static string ConstrollersAsCSV(string optional = null)
{
var list = GetControllerNames();
var sb = new StringBuilder();
sb.Append("(");
if (!string.IsNullOrWhiteSpace(optional))
sb.Append(optional);
foreach (var cont in list)
{
if (sb.Length > 1)
sb.Append("|");
sb.Append(cont.Replace("Controller", ""));
}
sb.Append(")");
return sb.ToString();
}
And the implementation of GetControllerNames is:
public static List<string> GetControllerNames()
{
List<string> controllerNames = new List<string>();
GetSubClasses<Controller>().ForEach(
type => controllerNames.Add(type.Name.ToLower()));
GetSubClasses<ApiController>().ForEach(
type => controllerNames.Add(type.Name.ToLower()));
controllerNames.Add("elmah");
return controllerNames;
}
And the implementation of GetSubClasses is:
private static List<Type> GetSubClasses<T>()
{
return Assembly.GetCallingAssembly().GetTypes().Where(
type => type.IsSubclassOf(typeof(T))).ToList();
}
The error I am getting when going to www.mywebsite.com/signup is:
**HTTP Error 403.14 - Forbidden**

Related

map routing not using default action

when I enter - http://localhost:60559/movies
The browser is redirecting to movies/index
Why? I have made the default action edit. All the names are just demo.
routes.MapRoute(
"searchByName",
"Movies/edit",
new {Controller = "Movies", action = "edit"}
);
actions are
public ActionResult index(int? id) {
if(!id.HasValue)
id = 2;
return Content("id: " + id);
}
public ActionResult edit(int? id) {
if (!id.HasValue)
id = 1;
return Content(String.Format("id = {0}", id));
}
the expected result is id = 1 in the browser but it is showing id: 2
Ive tried your code and it didnt work well but
Please try the following and you code should work just fine
routes.MapRoute(
name:"searchByName",
url: "{controller}/{action}",
defaults: new { Controller = "Movies", action = "edit" }
);
if you plane to add parameters then use following
routes.MapRoute(
name:"searchByName",
url: "{controller}/{action}/{id}",
defaults: new { Controller = "Movies", action = "edit", id = UrlParameter.Optional }
);
Please Add the following
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "SearchByName",
url: "Movies/{action}",
defaults: new { Controller = "Movies", action = "edit" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

ASP.Net MVC5: Extra segment in routing causes issue

to handle my routing issue i add extra segment in routing which not redirecting me to edit action.
see my routing which causing problem
routes.MapRoute
(
name: "PageWithId",
url: "Customers/Action/Edit/{page}/{id}",
defaults: new { controller = "Customers", action = "Edit" }
);
OR
routes.MapRoute
(
name: "PageWithId",
url: "Customers/Edit/Action/{page}/{id}",
defaults: new { controller = "Customers", action = "Edit" }
);
i test above 2 different set of routing for PageWithId but none work
see RouteLink code
#Html.RouteLink("Edit", "PageWithId",
new
{
controller = "Customers",
action = "Edit",
id = item.CustomerID,
page = ViewBag.CurrentPage
})
my edit action code
public ActionResult Edit(int page, string id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Customer customer = db.Customers.Find(id);
if (customer == null)
{
return HttpNotFound();
}
ViewBag.CurrentPage = page;
return View(customer);
}
now tell why this url http://localhost:2020/Customers/Action/Edit/1/AlFAKI not redirecting me to edit action?
see my full routing code
routes.MapRoute(
name: "PageWithSort",
url: "{controller}/{action}/{page}/{SortColumn}/{CurrentSort}",
defaults: new { action = "Index", page = UrlParameter.Optional, SortColumn = UrlParameter.Optional, CurrentSort = UrlParameter.Optional }
);
routes.MapRoute
(
name: "PageWithId",
url: "Customers/Action/Edit/{page}/{id}",
defaults: new { controller = "Customers", action = "Edit" }
);
OR
routes.MapRoute
(
name: "PageWithId",
url: "Customers/Edit/Action/{page}/{id}",
defaults: new { controller = "Customers", action = "Edit" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Your route looks wrong, I believe you are trying to do is:
routes.MapRoute(
name: "PageWithId",
url: "{controller}/{action}/{page}/{id}",
defaults: new { controller = "Customers", action = "Edit" }
);
And your url will be: http://localhost:2020/Customers/Edit/1/AlFAKI

One ASP.NET MVC route not working

I have a problem with routing, RouteConfig.cs contains these routes:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"TermsOfService",
"termsofservice",
new { controller = "Home", action = "TermsOfService" }
);
routes.MapRoute(
"PrivacyPolicy",
"privacypolicy",
new { controller = "Home", action = "PrivacyPolicy" }
);
routes.MapRoute(
"Contact",
"contact",
new { controller = "Home", action = "Contact" }
);
routes.MapRoute(
"Support",
"support",
new { controller = "Home", action = "Support" }
);
routes.MapRoute(
"ReadOurStory",
"readourstory",
new { controller = "Home", action = "ReadOurStory" }
);
routes.MapRoute(
name: "BlogItem",
url: "blog/{name}",
defaults: new { controller = "Home", action = "BlogItem" }
);
routes.MapRoute(
name: "ProductDetail",
url: "products/{name}",
defaults: new { controller = "Home", action = "Product" }
);
routes.MapRoute(
name: "Products",
url: "products",
defaults: new { controller = "Home", action = "Products" }
);
routes.MapRoute(
name: "Tutorials",
url: "tutorials",
defaults: new { controller = "Home", action = "Tutorials" }
);
routes.MapRoute(
name: "Blog",
url: "blog",
defaults: new { controller = "Home", action = "Blog" }
);
routes.MapRoute(
name: "TutorialDetail",
url: "tutorials/{name}",
defaults: new { controller = "Home", action = "Tutorial" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
and these two actions in HomeController:
public ActionResult Products()
{
var model = LoadItemsModel(ItemType.Product);
return View(model);
}
public ActionResult Tutorials()
{
var model = LoadItemsModel(ItemType.Tutorial);
return View(model);
}
Now the link for products is working:
http://localhost:61296/products/
but the link for tutorials:
http://localhost:61296/tutorials/
returns error
HTTP Error 403.14 - Forbidden
The Web server is configured to not list the contents of this directory
I tried to change the route just for a test to tutorials2 and the action to Tutorials2 and then this modified link is working. I do not know why the route tutorials does not work.
Based on the error, my guess would be that you have a physical directory in your project folder called "tutorials". Any physical files/directories will always overrule any routes. Then, since directory listing is disabled by default, you get that 403 error. Remove or rename the physical directory and you should be fine.

URLs which have not www, not work

I've changed my routing configuration to support some kinds of URLs, But none of them not work without www and show this error.
The resource cannot be found.
I can't found what's the problem here!
Routing:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "User",
url: "User/{lang}/{pageId}/{seo}/{parameters}",
defaults: new { controller = "Layout", action = "UserDashboardLayout", lang = "fa", pageId = 24, seo = "", parameters = "" }
);
routes.MapRoute(
"Module",
"ModuleCode/{controller}/{action}",
new { controller = "controller", action = "action", parameters = "" },
new string[] { "Shoniz.Website.Controllers.Module" }
);
routes.MapRoute(
name: "UserDashboard",
url: "{lang}/{pageId}/{seo}/{parameters}",
defaults: new { controller = "Layout", action = "MainLayout", lang = "en", pageId = 1, parameters = UrlParameter.Optional}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}"
,
defaults: new { controller = "Layout", action = "MainLayout", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "MainPage",
url: "{lang}",
defaults: new { controller = "Layout", action = "MainLayout", lang = "fa", pageId = 1, parameters = UrlParameter.Optional, seo = "Shoniz-Chocolate" }
);
}
}

Asp.net Routes with two routes

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.

Resources