I have the following routes defined in Global.asax.cs:
void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// catalog
routes.MapRoute("thumbs", "Thumbnail/{extn}/{image}/{width}/{height}", new
{
controller = "Image",
action = "FetchThumbnail",
extn = "gif",
image = MediaManager.DefaultImageGuid,
width = 250,
height = 200
});
routes.MapRoute("rfqlist", "Rfqs/", new { controller = "RfqList", action = "Index" });
routes.MapRoute("TappDefault", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
I cant step through and see them being registered. I have a controller:
public class RfqListController : TappController
public ActionResult Index(string rfqTitle, int? page)
If I call
> https://localhost:44300/rfqlist
it fires but if I call:
> https://localhost:44300/rfqs
it 404s. I cant see for looking what Im doing wrong. This is normally so easy.
do you have a rfqs controller?
https://localhost:44300/rfqs matches your default route ("TappDefault") which points to the Index action of rfqs controller
Related
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
}
The default url of the application is
http://servername/Root/ITProjects
There is a menu item with 'Team' on the default page which should redirect to another controller with index which will display a dropdownlist at the following url
http://servername/Root/ITProjects/Team
It's working fine so far.
However when the user selects an item in the dropdownlist, it should go to the url below
http://servername/Root/ITProjects/Team/Index?Id=7
But it's directing to
http://servername/Team/Index?Id=7
and throwing the 404 error. It is missing the folder path 'Root/ITProjects' after the servername. It's working fine on localhost where there is no folder path but failing on deployment to test or prod servers
[AuthorizeAD(Groups = "APP_xxxx_Users")]
public class TeamController : Controller
{
public int pageSize = 5;
private IProjectService _projectService;
public TeamController(IProjectService projectService)
{
this._projectService = projectService;
}
public ActionResult Index(int? Id, int? page)
{
int pageNumber = (page ?? 1);
var viewModel = new TeamViewModel();
if (Id != null)
{
viewModel.SelectedMember = (int)Id;
viewModel.Tasks = this._projectService.GetTasksByStaff(viewModel.SelectedMember).ToPagedList(pageNumber, pageSize);
}
return View(viewModel);
}
[HttpPost, ActionName("Index")]
public ActionResult IndexPost(int Id, int? page)
{
int pageNumber = (page ?? 1);
var viewModel = new TeamViewModel();
viewModel.SelectedMember = Id;
if (ModelState.IsValid)
{
viewModel.Tasks = this._projectService.GetTasksByStaff(viewModel.SelectedMember).ToPagedList(pageNumber, pageSize);
}
return View(viewModel);
}
}
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 }
);
}
As part of Index view when dropdownlist item is selected
#Html.DropDownListFor(m => m.SelectedMember, new SelectList(Model.StaffList, "UniqueId", "Initials", Model.SelectedMember), new { #class = "btn btn-default btn-color", onchange = #"form.action='/Team/Index?Id=' +this.value;form.submit();" })
Your MVC application is in a sub-directory of another application, so IIS considers the root of the parent application instead of the root of your MVC application.
In IIS, you need to convert the virtual directory to an Application: choose your virtual directory and "Convert to Application." If you are unable to do that, then you need to modify your routes in routes.config to take the virtual directory into account:
routes.MapRoute(
"Default", // Route name
"Root/ITProjects/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
your default route for controllers looks like this :
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
You can add /Root/ITProjects/ before the URL to change the default controller path
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 }
);
}
}
How to change nopcommerce default action?
I create new action in HomeController, and want to be default page.
I change:
routes.MapRoute(
"",
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "Nop.Web.Controllers" }
);
To:
routes.MapRoute(
"",
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "NewAction", id = UrlParameter.Optional },
new[] { "Nop.Web.Controllers" }
);
But nothing has changed.
When you navigate to /Home/Index, MVC parses route as follows:
Controller: Home
Action: Index
Id:
If you navigate to /Home:
Controller: Home
Action: NewAction (from route's default action)
Id:
You can make it always activate NewAction like this:
routes.MapRoute(
"",
"{controller}/{id}", // URL with parameters
new { controller = "Home", action = "NewAction", id = UrlParameter.Optional },
new[] { "Nop.Web.Controllers" }
);
You can try like this.
//your default action
public ActionResult Index()
{
return RedirectToAction("NewAction"); //Like Response.Redirect() in Asp.Net WebForm
}
//your new action
public ActionResult NewAction()
{
//some code here
return view();
}
Request ["fileName"] return null?? Why?
Full image: http://i.stack.imgur.com/U1MzX.jpg
Controller:
Full image: http://i.stack.imgur.com/DCQNG.jpg
Route
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"AdminLogin",
"Admin/",
new { controller = "Account", action = "Login" }
);
//NossaIgreja/{nomePastor}
routes.MapRoute(
"Pastor", // Route name
"Pastor/{id}", // URL with parameters
new { controller = "NossaIgreja", action = "Pastor" } // Parameter defaults
);
routes.MapRoute(
"Download", // Route name
"Downloads/Boletim/{year}/{week}", // URL with parameters
new { controller = "Downloads", action = "DownloadBoletim" }, // Parameter defaults
new { year = #"\d+", week = #"\d+" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
, new string[] { "SextaIgreja.Web.Controllers" }
);
}
Area registration
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Admin";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new string[] { "SextaIgreja.Web.Areas.Admin.Controllers" }
);
}
}
Your controller action should take "fileName" parameter, instead of doing a Request["fileName"]. As long as your query string parameter matches the name of the parameter in your controller mvc should pass it in automatically.
If Remover is an MVC action than I cannot see the parameter in Remover Action called fileName
It means when call Remover action the fileName in your querystring is not getting automatic model binding