C# MVC RedirectToAction not working? - asp.net-mvc-3

So I want to create a new view in my MVC application that allows a user to enter parameters for searching. I want to pass these parameters to another View/Controller and I want the controller to call an action called "Search" to handle these parameters and return the correct data. However, when I try to "Redirect" it is giving me a problem. It says the resource cannot be found,
The view 'Search' or its master was not found or no view engine supports the searched locations.
The following locations were searched:
~/Views/Question/Search.aspx
This is the code.
[HttpPost]
public ActionResult HandleForm()
{
SearchQuery search = new SearchQuery();
if(Request["QuestionID"].Trim()!="")
search.QuestionID = Convert.ToInt32(Request["QuestionID"].Trim());
return RedirectToAction("Search", "Question");
}
However, if I change "Search" to "Index" it loads the page I desire because it opens the view within that page. It does not call the search action. Why is this method returning the View when every example I've read states that the name of the Action needs to be passed?
For those who are wondering this is my global.asax routing info
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
Last but not least, I have yet to look into how to pass these parameters, but I hope it won't be too much extra work once I can figure out why this is not working as desired.

Go to the Views/Questions directory and make sure there is a file called Search.cshtml. If it does exist also then make sure that this view has a corresponding action method, something like:
public class QuestionController : Controller
{
public ActionResult Search()
{
}
}

If you are in same controller then write:
return RedirectToAction("Search");
or if your search action is in other controller then write:
return RedirectToAction("Search","your Controller Name Here");

Related

ASP.NET MVC3 sample project removing home folder

I'm using the ASP.NET MVC3 sample project and would like to have new links added to the page that go directly to the root url
So instead of mydomain.com/Home/About it would do mydomain/About.
This page suggests adding a new route. http://weblogs.asp.net/gunnarpeipman/archive/2011/04/17/asp-net-mvc-defining-short-urls-for-root-level-pages.aspx
Is there another way? Say I have 5 pages that will be on the root do I have to add a special route for each one?
Under the assumption that you are looking to do a bunch of single path requests/respones, and not just redirect home controller actions, then this is an option.
The link you provide is one way to do that. The other is to create 5 controllers using the default route. I'm not sure if I would suggest either is better (due to a lack of what your 5 paths actually are), but they both produce the same out come. If your default route looks like:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index",
id = UrlParameter.Optional } // Parameter defaults
);
It's basically stating that the default controller is home and the default action is index. These values are not mutually inclusive, meaning that neither required the other in order to be a default value.
Thus you could do:
website.com/about with
public AboutController
{
public ActionResult index()
{
return this.View();
}
}
and/or website.com/people with
public PeopleController
{
public ActionResult index()
{
return this.View();
}
}

Recommended API design with ASP.NET MVC3

I'm working with ASP.NET MVC 3. I'm kind of new to it. I think I'm starting to get the hang of it. But there is something that I'm trying to do, that I think makes sense, but maybe I'm wrong.
I'm trying to create an API around Order objects in my database. In order to get all of the orders in the system, I was going to expose an API that looks like the following:
/orders/
In cases where I wanted to get a specific Order, I would simply append an ID. In other words, the URL would look like this:
/orders/12345
In an effort to accomplish this, I created the following controller:
public class OrdersController : Controller
{
// GET: /Orders/
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Index()
{
string result = "list of orders";
return Json(result, JsonRequestBehavior.AllowGet);
}
//
// GET: /Orders/{orderID}
public ActionResult Index(int id)
{
string result = "order:" + id;
return Json(result, JsonRequestBehavior.AllowGet);
}
}
In my AreaRegistration class, I have the following:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"OrderList",
"{controller}/{action}",
new { action = "Index", controller="Orders" }
);
context.MapRoute(
"Order",
"{controller}/{action}/{id}",
new { action = "Index", controller = "Orders" }
);
}
When I attempted to access "/orders/", via the browser address bar, I get the JSON like I would expect. However, if I attempt to access "/orders/12345", I receive a 404. What am I missing?
Thank you
You need to also define proper routes in global.asax or use the default route which looks like {controller}/{action}/{id} where controller is defaulted to "Home", action is defaulted to "Index" and id is optional.
So /orders works because you have defined controller (orders), default action (Index) and missing id (which doesn't matter as it is optional)
But when you try /orders/12345 then you have defined controller (orders), action (12345) and missing id
So to make this work with only the default route the request should be /orders/index/12345
edit: for registering area routes you should use AreaRegistration class

ASP MVC Razor Render Partial Not Finding The Correct Route Details

I am trying to run #{ Html.RenderAction("List", "Template"); } in my parent view.
My parent view maps to controller = EmailTemplateHost Action = Index
Which I have registered using:
routes.MapRoute(
"TemplateIndex",
"Template",
new { controller = "EmailTemplateHost", action = "Index" }
);
Template/List should map to controller = EmailTemplateList action = List
Which I have registered using:
routes.MapRoute(
"TemplateList",
"Template/List",
new { controller = "EmailTemplateList", action = "List" }
);
All of the routes are in the RouteTable. However upon rendering my Index page when it hits the RenderAction it hits the Windsor Controller Factory asking for the controller name of Template and not EmailTemplateList. It seems as if it isn't going through the routing engine. If I look in the request context the route data values is showing Template and List in the dictionary but it is not showing the mapping I require to find the correct controller.
Oh yea if I use #{ Html.RenderAction("List", "EmailTemplateList"); }
It works!
Any thoughts?
Thanks Jonathan.
Put the second route TemplateList above the TemplateIndex route in your global.asax.

MVC3 Area +Authorize attribute + Role strange issue

I really don't know what title should I use to describe my problem. To simplify my problem. Here is my test. I create a mvc3 site from scratch. I then add area called "admin". Inside admin, I have a controller named "Search" and has "Authorize" attribute decorated. I then changed my Global.ascx.cs route setting to append my controller namespace. Now I start my test.
Question 1
When I am accessing to http://localhost:xxx/Search page, it redirects me back to /Account/Logon page, it makes me confuse first, why it redirects me to logon page? it shouldn't reach to Admin search controller at all as I understand. If I removed the Authorize attribute, it display the yellow screen said can't find the view as I expected.
Question 2
If I add Authorize attribute with role, e.g. (Roles="Admin"), then I try access to Search page again, no matter login succeed or not, I always get redirect back to logon page. Why it doesn't give me the yellow screen, coz I am trying to request the search controller index view in the main site not the admin area's one. quite confuse.
I am a newbie in MVC development, can someone give me a solution regarding to my problem?
Thanks
Global.ascx.cs
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[]{"TestAreaRouting.Controllers"}
);
}
You could constrain the default controller factory to look only inside the specified namespace for controllers in the RegisterRoutes method of Global.asax by setting the UseNamespaceFallback data token to false:
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 string[] { "TestAreaRouting.Controllers" }
).DataTokens["UseNamespaceFallback"] = false;
}
If you don't do this when you request /search the admin area route doesn't match because the url doesn't start with the Admin prefix.
So it is the default route that matches. The default controller factory starts scanning the assembly for a class called SearchController that derives from Controller and since it finds one it instantiates it and uses it to serve the request. Obviously it doesn't find a corresponding Index view because it looks in ~/Views/Search/Index.cshtml which obviously doesn't exist. The actual view is located in the area.
Now that we have constrained the controllers to their respective locations you could decorate them with the Authorize attribute and it should behave consistently.

ASP.Net Mvc 3 Url.Action method uses parameter values from previous request

When Urls are autogenerated using the Url.Action helper, if a page contains a line similar to
#Url.Action("Edit","Student")
is expected to generate a url like domain/student/edit and its working as expected.
But if the requested url contains some parameters, like domain/student/edit/210, the above code uses these parameters from the previous request and generates something similar even though I've not provided any such parameter to the Action method.
In short, if the requested url contains any parameters, any auto generated links of the page (served for that request) will include those parameters as well no matter if I specify them or not in the Url.Action method.
What's going wrong?
Use Darin's answer from this similar question.
#Url.Action("Edit","Student", new { ID = "" })
Weird, can't seem to reproduce the problem:
public class HomeController : Controller
{
public ActionResult Index(string id)
{
return View();
}
public ActionResult About(string id)
{
return View();
}
}
and inside Index.cshtml:
#Url.Action("About", "Home")
Now when I request /home/index/123 the url helper generates /home/about as expected. No ghost parameters. So how does your scenario differs?
UPDATE:
Now that you have clarified your scenario it seems that you have the following:
public class HomeController : Controller
{
public ActionResult Index(string id)
{
return View();
}
}
and inside Index.cshtml you are trying to use:
#Url.Action("Index", "Home")
If you request /home/index/123 this generates /home/index/123 instead of the expected /home/index (or simply / taken into account default values).
This behavior is by design. If you want to change it you will have to write your own helper which ignores the current route data. Here's how it might look:
#UrlHelper.GenerateUrl(
"Default",
"index",
"home",
null,
Url.RouteCollection,
// That's the important part and it is where we kill the current RouteData
new RequestContext(Html.ViewContext.HttpContext, new RouteData()),
false
)
This will generate the proper url you were expecting. Of course this is ugly. I would recommend you encapsulating it into a reusable helper.
Use ActionLink overload that uses parameters and supply null
You could register custom route for this action for example:
routes.MapRoute("Domain_EditStudentDefault",
"student/edit",
new {
controller = MVC.Student.Name,
action = MVC.Student.ActionNames.Edit,
ID = UrlParameter.Optional
},
new object(),
new[] { "MySolution.Web.Controllers" }
);
you then could use url.RouteUrl("Domain_EditStudentDefault") url RouteUrl helper override with only routeName parameter which generates url without parameters.

Resources