I have a problem in my MVC project were the first time I request a page it makes a redirection to another area, but the second time I request it works correctly.
The url I use is: http://localhost:2012/pt/Documentation/Index and redirects me to http://localhost:2012/es/Services/Documentation
I've debugged the code and in both requests the url asked for is the first, and the action that respond the request is correct, but once the reply gets to the browser , the first time is recieved with a 302 code and it redirects me to this second URL.
I've checked the web.config with any redirection rule but there aren't.
The Area registration is:
routes.MapRoute(
name: "Default",
url: "{language}/{controller}/{action}/{id}",
defaults: new { language = "es", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
And the controller is like:
public class DocumentationController : Controller
{
public ActionResult Index(string language)
{
return View();
}
}
The setCulture is an atribute that is used to change the translations needed in the view.
I've tried to comment the view and the layout but it still redirects without code.
Any help will be aprecciated.
****EDIT****
First request:
Second request:
The view code is:
#model string
#{
ViewBag.Title = "Resend documentation";
Layout = null;
}
Related
I guess I don't completely understand how urls work with C# projects, in the sense that I don't know how to specify a url to go through the controller and not just return a aspx page.
Say I am trying to get to my project's Index page through a Controller named "ScholarshipController.cs". I would think to hit the Index method/action in this controller, my url would be as follows (my app's name is "TuitionAssistance" fyi):
http://localhost/TuitionAssistance/Scholarship/Index
However, running this url just returns the aspx page named "Index.aspx" located in the "Scholarship" view file without hitting the Controller. Why is this happening, and how do I get it to go through the controller so the Index page, when loaded, will have the appropriate information loaded onto it?
Sorry if this is a dumb question. Any insight would be appreciated. Thanks!
Route.config:
using System.Web.Mvc;
using System.Web.Routing;
namespace ScholarshipTuitionAssistance
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
/* Scholarship */
/* Scholarship */
//routes.MapRoute("TuitionAssistance",
// "tuition/{name}",
// new { controller = "TuitionAssistance", action = "Index", name = "" });
routes.MapRoute(
name: "TuitionAssistance",
url: "{controller}/{action}/{employee_number}",
defaults: new { controller = "Home", action = "TuitionAssistance", employee_number = UrlParameter.Optional }
);
routes.MapRoute(
name: "Scholarship",
url: "{controller}/{action}/{employee_number}",
defaults: new { controller = "Home", action = "Scholarship", employee_number = UrlParameter.Optional }
);
routes.MapRoute(
name: "Details",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Scholarship", action = "Details", id = UrlParameter.Optional }
);
}
}
}
Your route (URL) cannot match anything that actually exists on the filesystem. In your example here, you apparently have a file, [document root]\Scholarship\Index.aspx. As a result, a request for Scholarship/Index will return that file, instead of invoking the ASP.NET MVC machinery to load a controller action.
In MVC ASP.NET, think of those types of links as a way to call your methods in your controller. When that link is accessed, your controller does a bunch of junk and then returns an ActionResult (or other things). This ActionResult, for the sake of this explanation, is the markup that is written in the corresponding view file. Controller - >index() will return the view called index under views - > controller. If you want to pass information to your view, you will pass a model that has all of your information in it to the view from your index controller (return View(MyFancyModel)). The view will have a razor line at the top such as: #model The.Namespace.Wherever.my.model.is
The scaffolded controllers and views in Visual Studio for the index page specifically, only pass a list of the items in the corresponding database.
I've recently added Areas to an existing MVC 4 web app. One of my areas has a Home controller, so obviously when I navigate to /MyArea/Home/Index I want to display it's Index view. Initially I was getting the following error:
Multiple types were found that match the controller named 'Home'. This can happen if the route that services this request ('{controller}/{action}/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.
The request for 'Home' has found the following matching controllers:
MyApp.Controllers.HomeController
MyApp.Areas.MyArea.Controllers.HomeController
Researching the issue I found that I needed to add default namespaces to my calls to MapRoutes() and that's stopped the error. Unfortunately I now find that when I go to /MyArea/Home/Index the app actually displays the view for /Home/Index instead - I can't display actions from the Area's Home controller.
Here's my code:
Global.aspx.cs
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
MyApp.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},
namespaces: new[] {typeof(MyApp.Controllers.HomeController).Namespace}
);
}
MyApp.Areas.MyArea.MyAreaRegistration
public override void RegisterArea(AreaRegistrationContext context)
{
var ns = typeof(MyApp.Areas.MyArea.Controllers.HomeController).Namespace;
var routeName = AreaName + "_Route";
var routeUrl = AreaName + "/{controller}/{action}/{id}";
var routeDefaults = new {controller = "Home", action = "Index", id = UrlParameter.Optional};
var routeNamespaces = new[] {ns};
context.MapRoute(
name: routeName,
url: routeUrl,
defaults: routeDefaults,
namespaces: routeNamespaces
);
}
Anyone got any bright ideas on how to solve this?
Update:
The problem only occurs when calling an action that exists in both HomeController classes, like Index. Calling an action in the Area's HomeController that does NOT exist in the default HomeController displays the correct view.
Update 2:
Turns out that this was a classic PICNIC error - a simple typo in my code, so the action went looking for a view that did not exist. As such, it went for the first matching view it could find - the one in the "root"
You need to add new { Area = "MyArea" } in the routeValues parameter when you are linking to the one in the MyArea area.
In your RegisterArea function you should update the routeDefaults to also include the area name.
var routeDefaults = new {controller = "Home", action = "Index", area = AreaName, id = UrlParameter.Optional};
<facepalm>
In the process of importing the code for the Area I made a typo in the view's folder name, so MVC wasn't able to find the correct index.cshtml for the area action.
</facepalm>
By the looks of things, when MVC can't find the area action's view it uses the default view instead. Once I'd fixed the name of the folder in Areas\MyArea\Views to match the controller name everything worked as expected.
+1 to both of the folks who answered - your responses were helpful and I've employed both in my updated code
I have a service in my website that loads content blocks from an external provider, from which users are are able to click links and navigate.
My routing needs to be able to handle these by calling my home controller with the request path.
For example, the url they will use to navigate will be
www../shop/hire/category/subcategory/subsubcategory/....
and if they're after a specific product:
www../shop/hire/category/subcategory/subsubcategory...?product=ABC
the constant in that, would be /shop/hire/ with the categories changing based on where you are, and the product if they have found what they are actually after.
The problem I've got, is when a link with a path like that is clicked in my application, rather than using the HomeController so I can parse the request, and call the service with the appropriate URL, I just get a 404.
I've tried adding the route:
routes.MapRoute(
name: "Category",
url: "shop/hire/{categories}/{product}",
defaults: new { controller = "Home", action = "Index", categories = UrlParameter.Optional, product = UrlParameter.Optional }
);
but this doesn't seem to have had any effect.
Try using a catch all route
routes.MapRoute(
name: "Category",
url: "shop/hire/{*categories}",
defaults: new { controller = "Home", action = "Index" }
);
and in your action you can parse the value to get your categories and product
public ActionResult Index(string catagories) { ... }
I would like to have some paramaters after my website's URl.
So, mysite.com/ThisIsMyStringMessage, would show this :
Hello, ThisIsMyStringMessage.
Of course, the view related to the action contains this :
Hello, #ViewBag.Message
And the HomeController :
public ActionResult Index(string message)
{
ViewBag.Message = message;
return View();
}
Edit related to comments :
#Zim, I'll have more than one controller.
#Jessee, So that's what I'm doing here, thank you, I didn't know really what I was doing.
With the default routing, ThisIsMyStringMessage will be picked up as the Controller, not as an Action method parameter. You will need a custom route to be able to do this. Try the following before the default route in Global.asax:
routes.MapRoute(
"RootWithParameter",
"{message}",
new { controller = "Home", action = "Index", message = UrlParameter.Optional }
);
Alternatively, access the method with mysite.com?message=ThisIsMyStringMessage
You will need to take a look at your RouteConfig.cs or Global.asax to setup a route with the parameter of message instead of id. Right now it probably looks like:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
You would need to change the url parameter to something like {controller}/{action}/{message} for that to work.
Also, be careful with passing strings in the URL. ASP.NET (and some modern browsers) gives you many security protections to prevent people from doing nasty stuff like XSS, but it's probably not a habit you want to create.
I thought I had at least a bases for understanding routing in MVC after all the documentation I have read, only to fail at it when attempting to utilize it.
I have the following two routes declared in my Global.aspx
routes.MapRoute(
"", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Admin", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
I have a AdminController that I have a few methods, one that is "List" method that renders a list of products to a "List" view.
I thought I understood how RedirectToAction works, and I added an "Add" method (see below) that adds a new Product and returns a RedirectToAction which I understood would be the proper way to redirect to the List action on the same "AdminController"
[HttpPost]
public ActionResult Add(Product product) {
if (_prodRepo.Add(product)) {
return RedirectToAction("List", "Admin");
}
return View("Add", product);
}
However, on the return of the "Add" it always attempts to route to the path website.com/Account/Login/ReturnUrl=%2f.
However, if I go to website.com/Admin it does render the list as I expect. But when using the RedirectToAction as in the example above, it attempts to go to the /Account/Login (Controller/action).
It was my understanding that the RedirectToAction("List", "Admin") would route to the "List" method on the AdminController controller and that I was using it as expected.
Can someone please help me understand the reasoning behind this. But also, could someone post some recommended articles for understanding the whole MVC routing including how the web.config works with routing.
Finally, it was also my understanding that route discovery by the framework is done in the order they are specified in your routes.MapRoute() declaration and stops at the first one that matches. Therefore, if the first one is listed as Controller = "Admin", Action = "List", I would expect by convention that this is the correct route it would first match and return.
Your routes need to be different (the url parameter) since the first route with a matching url will be used.
This will therefore work for you:
routes.MapRoute("Admin",
"admin/{action}/{id}",
new { controller = "Admin", action = "List", id = UrlParameter.Optional });
The defaults (third parameter in the method) are used if the parameters isn't found/specified in the uri.
As for your question regarding /Account/Login/ReturnUrl=%2f. The login redirections are handled by the MembershipProvider and not by the standard routing mechanism.