My url: http:///Home/addorders //Working Fine
Need to hit like this: http:///addorders
I have added my customized route in RouteConfig: But it does not works for me
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection 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(
"addorders", // Route name
"addorders/{id}/{str}", // URL with parameters
new { controller = "Home", action = "addorders", id = UrlParameter.Optional, str = UrlParameter.Optional },// Parameter defaults
new[] { "AngularJsExample.Controllers" }
);
}
}
MVC5 provides new feature called Attribute Routing. Instead of registering your routes in Routing Config, You can directly specify routing controller level or action level.
For example
RouteConfig.cs
using System.Web.Routing;
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();// New feature
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Home Controller code
[RoutePrefix("Test")]
[Route("{action=Index}")]// Default Route
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[Route("HelloAbout")] //Test/Users/About
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
[Route("~/NewTest/Contact")] //To override the Route "Test" with "NewTest".... NewTest/Contact
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
//[Route("home/{id?}")]
public ActionResult Emp(int id)
{
return View();
}
}
This code will change the controller and action name and also change the routing.
Add the new, more specific route before the default in the file (which determines the order it is added to the route dictionary). This is because routes are evaluated for a match to an incoming URL in order. The default MVC route is pretty general, and if route order is not carefully considered when adding additional routes to your project, the default route may inadvertently match a URL intended for a different handler.
Modified code:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"addorders", // Route name
"addorders/{id}/{str}", // URL with parameters
new { controller = "Home", action = "addorders", id = UrlParameter.Optional, str = UrlParameter.Optional },// Parameter defaults
new[] { "AngularJsExample.Controllers" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Related
So I want to get my URLs to look something similar to this:
example.com, example.com/contact, example.com/sign-up, example.com/account
Instead of the default MVC way which would look like:
example.com, example.com/Home/Contact, example.com/Account/SignUp, example.com/Account/Account
All HomeController views work fine, but my AccountController doesn't.
When going to example.com/account I get an error saying:
The resource cannot be found.
This is my RouteConfig:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.LowercaseUrls = true;
routes.MapRoute(
name: "Default",
url: "{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Account",
url: "{action}/{id}",
defaults: new { controller = "Account", action = "Account", id = UrlParameter.Optional }
);
}
And because I wanted to display the Account/SignUp view as /sign-up I added some custom code in the AccountController.
// GET: Account
public ActionResult Account()
{
return View();
}
// GET: Sign Up
[ActionName("Sign-Up")]
public ActionResult SignUp()
{
return View("SignUp");
}
Both formats /account or /account/account give me the same error from earlier.
However /about, /contact, etc. does not.
Any help is appreciated, thanks!
P.S This is my folder structure:
> Views
-> Account
--> Account.cshtml
--> SignUp.cshtml
-> Home
--> About.cshtml
--> Contact.cshtml
--> Index.cshtml
Route setup would need to be more explicit.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.LowercaseUrls = true;
routes.MapRoute(
name: "Account",
url: "account/{action}/{id}",
defaults: new { controller = "Account", action = "Account", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Maybe custom route should be before default route
Add RouteConfig.cs:
routes.MapMvcAttributeRoutes();
Add AccountController.cs as an attribute to the SignUp method
[Route("Sign-Up")]
Finally:
RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
AccountController.cs
[Route("Sign-Up")]
public ActionResult SignUp()
{
ViewBag.Message = "Your application description page.";
return View();
}
Result
I am working with MVC5 where I am unable to put dashes in the URL. What I want is to put a dash between the words.Kindly view my working code.
My Route Config is:
routes.Add(
new Route("{MyApple}/{page}",
new RouteValueDictionary(
new { controller = "MyApple", action = "Index", page = "" }),
new HyphenatedRouteHandler())
);
And My controller is :
public ActionResult Index(string page)
{
if (System.IO.File.Exists(Server.MapPath("/Views/MyApple/" + page + ".cshtml")))
{
string userId = "";
if (Session["AppId"] != null)
userId = Session["AppId"].ToString();
Session["ReturnUrl"] = Url.Action("EmptyTemplate");
IBaseViewModel model = new BaseViewModel();
return View(page, model);
}
}
This is my controller where the parameters go to page.
Example: devpage = aspdotnet then it will render the view = aspdotnet.html
Please help with this edited content.
I want to dash to the URL with lowercases.
Example: Suppose I have perfectsolution.html page then i
localhost/case-study/perfect-solution
(Don't mind my English).Help will be appreciated. Thanks
Note: in the index page, I` am using "perfect solution" to pass to the view but I want in URL, perfect-solution.
I want this type URL /case-studies/san-Diego-veg-festival ,
casestudies is controller and san-diego-veg-festival is parameter.
As per your comments , I made a guess that you are trying to pass parameter value in CaseStudies controller's Index method.
Two types of Routing to choose from, you can choose anyone as required:
1. Convention-based Routing
In Your RouteConfig.cs file make these changes:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "CaseStudiesRoute",
url: "case-studies/{devPage}",
defaults: new { controller = "CaseStudies", action = "Index", devPage = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Controller:
public class CaseStudiesController : Controller
{
public ActionResult Index(string devPage)
{
return View();
}
}
Now using your browser give a GET request to URL , /case-studies/san-Diego-veg-festival , It should work with Convention-based Routing.
2. Attribute Routing
In Your RouteConfig.cs enable Attribute Routing , by adding routes.MapMvcAttributeRoutes(); , Now RouteConfig.cs looks like:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Controller:
public class CaseStudiesController : Controller
{
[Route("case-studies/{devPage?}")]
public ActionResult Index(string devPage)
{
return View();
}
}
Now using your browser give a GET request to URL , /case-studies/san-Diego-veg-festival , It should work with attribute routing.
References
1.
2.
Try using Routing Attribute functionality
[Route("study/perfect-solution")]
eg:
[Route("study/perfect-solution")]
public ActionResult PerfectSolution()
{
// code here
}
If you want to pass parameters it need to be specified
Eg:
[Route("study/perfect-solution/{parameter}")]
public ActionResult PerfectSolution(string parameter)
{
// code here
}
I want my route to look like:
/product/123
I have an action GET but I don't want that in the URL, it is currently:
/product/get/123
How to get this?
Global.asax.cs
RouteConfig.RegisterRoutes(RouteTable.Routes);
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "MyApp.Web.Controllers" }
);
}
You can use the Route attribute to define your path like this:
[Route("product")]
public class ProductController {
[Route("{productId}"]
public ActionResult Get(int productId) {
// your code here
}
}
Which provides you the full route definition for "/product/{productId}" which is "/product/123" in your case. More details there: https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/
Add this code in routes.MapRoute before the default route.
routes.MapRoute(
name: "Product",
url: "Product/{id}",
defaults: new { controller = "product", action = "get"}
I am trying to defined custom routing in MVC5 like below. But when I call http://company.com/protected/Myaccount is not working. What I am doing wrong.
and also how should defined default load with http://company.com/protected. Now it is loading http://company.com/
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Protected",
url: "protected/{controller}/{action}/{id}",
defaults: new { controller = "MyAccount", action = "Index", id = UrlParameter.Optional });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {controller = "MyAccount", action = "Index", id = UrlParameter.Optional});
}
[RoutePrefix("Protected")]
[Route("{action=index}")]
public class MyAccountController : Controller
{
// GET: MyAccount
public ActionResult Index()
{
...
}
}
Edit: I forgot the second part of your question.
[RoutePrefix("Protected")]
public class MyAccountController : Controller
{
[Route("Index")] //Route: /Protected/Index
[Route("")] //Route : /Protected
[Route("~/")] //Route : /
public ActionResult Index()
{
return View();
}
}
With [RoutePrefix] you change the access to your controller, with [Route] you can change your access to your action (ex:parameter and order) and defined default for the controller and your app.
In this case, i delete the "Protected" route on RegisterRoute because it's not used. The RouteAttributes is another solution
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
//routes.MapRoute(
// name: "Protected",
// url: "Protected/{Controller}/{action}/{id}",
// defaults: new { controller = "MyAccount", action = "Index", id = UrlParameter.Optional }
//);
//routes.MapRoute(
// name: "Default",
// url: "{controller}/{action}/{id}",
// defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
//);
}
In fact, attributes routing is another way to define routing, it's more flexible.
I am trying to create a clean URL path to an MVC controller and I need a little assistance.
Example desired path: http://www.linkedin.com/in/alumcloud. This path is from LinkedIn and notice how it has the /in/alumcloud.
I would like for mine to read: http://www.alumcloud.com/alumcloud/somecompanyname
How would I do this with an MVC controller?
Below is the only code in my MVC controller, becuase this controller is only needed to respond to GET HTTP methods.
public class AlumCloudController : AlumCloudMvcControllerBase
{
// GET: /AlumCloud/Details/5
public ActionResult Details(string companyName)
{
return View();
}
}
--Here is my RouteConfig
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
-----------------------------------2nd Attempt------------------------------------
--Route Code 2
public static void RegisterRoutes(RouteCollection 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(
name: "Public",
url: "ac/{companyName}",
defaults: new { controller = "AlumCloudController", action = "UserProfile" }
);
}
--Controller Code 2
public class AlumCloudController : AlumCloudMvcControllerBase
{
// GET: /AlumCloud/Details/5
public ActionResult UserProfile(string companyName)
{
return View();
}
}
--The URL
'/ac/' + options.person.CompanyName
--Snap shot of 404 error
Try adding this route above your default:
routes.MapRoute(
name: "Profile",
url: "alumcloud/{companyName}",
defaults: new { controller = "AlumCloudController", action = "Details" }
);