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" }
);
}
}
Related
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 }
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
I have created multiple routes with different route name in MVC.
routes.MapRoute(
name: "PostDetails",
url: "Ad/{id}/{item}",
defaults: new { controller = "Home", action = "Post" }
);
I am calling route from a javascript function to redirect to this route
var url = '#Url.RouteUrl("PostDetails", new { id = "_id_", item = "_name_" })';
url = url.replace("_id_", id).replace("_name_", name);
window.location.href = url;
This is giving an error with 404.
You must add your route before Default route in the RouteConfig like:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "PostDetails",
url: "Ad/{id}/{item}",
defaults: new { controller = "Home", action = "Post" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
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**
routes.MapRoute(
name: "GetAdressen",
url: "{controller}",
defaults: new { controller = "AdressenController", action = "GetAdressen"}
);
routes.MapRoute(
name: "GetEinsaetze",
url: "{controller}",
defaults: new { controller = "EinsaetzeController", action = "GetEinsaetze"}
);
In this case only /Adressen will work, and not also /Einsaetze
routes.MapRoute(
name: "GetEinsaetze",
url: "{controller}",
defaults: new { controller = "EinsaetzeController", action = "GetEinsaetze"}
);
routes.MapRoute(
name: "GetAdressen",
url: "{controller}",
defaults: new { controller = "AdressenController", action = "GetAdressen"}
);
In this case only /Einsaetze will work, and not also /Adressen
Why?
Use Like This
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "projectName.Controllers" });