Action method is not fired after deploying in IIS using Ajax.ActionLink() - asp.net-mvc-3

I have a default routing as below:
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" } // Parameter defaults
);
}
My Index Action method is returning a list of model to view. And in Index View i have somthing like this:
#Html.ActionLink(article.Title,"Download",new { id = "C:\\Files\file1.txt"},null);
But, AFter deploying in IIS the "Download" action method is not getting fired And getting a 404 PAGE NOT FOUND error.
If I am specifing the action method as 'Index" in Html.ActionLink, Then It's working Like:
#Html.ActionLink(article.Title,"Index",new { id = "C:\Files\file1.txt"},null);
The Above code is working because it's taking the default route after deploying in IIS. I tried to change the routes in Global.asax, But failed to get a solution.
What i need here is, I want the second action method to be fired, which is not.
My 1st Action Method is :
public ActionResult Index() {}
My 2nd Action Method is :
public ActionResult Download(string loc) {} //This is not getting fired..???
Will provide you more details if needed..please Assist, as it will effect my delivery.

Related

Server Error in '/' Application MVC3

im not sure what i messed up, but i just keep getting the following error upon f5.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /
The following is my route, totally default and no changes.
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 } // Parameter defaults
);
}
I have checked my project properties -> web tab, "Specific page" has nth. My project has Home folder with Index page.
Other pages are working only after manually inputting URL. For eg: http://localhost:21183/store/search
Thanks
Things to check:
You have a public class named HomeController that derives from Controller.
This HomeController class has a public Index action.
You have a corresponding view ~/Views/Home/Index.cshtml
You are testing this inside a web server which supports extensionless urls. For example this won't work out of the box in IIS 6.0.
Controller:
public class HomeController: Controller
{
public ActionResult Index()
{
return View();
}
}

Incorrect Routing is ASP.NET MVC

I don't know why I have such problems with ASP.NET MVC routing. I wish there was a tool that showed me which routes I had currently setup. Regardless,
In my global.asax.cs file I have the following:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"SignUp", // Route name
"account/{controller}/{action}", // URL with parameters
new { controller = "Home", action = "Register" } // 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 the following defined in HomeController.cs
public ActionResult Register()
{
return View();
}
I was expecting to be able to access this page by visiting /account/register in my browser. However, I continue to get a 404. What am I doing wrong?
/Account/Register matches your first route.
The word Register is matched to the {controller}, so it looks for a controller named RegisterController.
replace
routes.MapRoute(
"SignUp", // Route name
"account/{controller}/{action}", // URL with parameters
new { controller = "Home", action = "Register" } // Parameter defaults
);
with
routes.MapRoute(
"SignUp", // Route name
"account/{action}", // URL with parameters
new { controller = "Home" } // Parameter defaults
);
This will mean /account/register will route to the Register action on the Home controller. It will also mean that action links and other links you generate via #Html.ActionLink("Register", "Register", "Home") will generate the URL /account/register
Think of the 'URL with paramters' as a pattern that the URL will be matched against.
The problem with your original route map is that it is looking for a URL like this /account/controllername/actionname. So, when you go /account/register - it is taking register as the controller name, and taking the default action name (in this case register) - and as the 'register' action does not exist in the 'register' controller - you are getting a 404.
UPDATED
I updated my suggested route as per Robert's comments.
It is also worth noting, as Robert states, that this whole thing could be made more simple by making a 'Account' controller, and moving the 'Register ' action there. Then you could delete the 'SignUp' route, and just use default routing. If you thought about it, you'd agree that this would be a better place for a 'Register' action than the 'Home' controller.
Try using this nugget package http://nuget.org/packages/Glimpse.Mvc3
You can find more info about glimpse on http://getglimpse.com/

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.NET MVC 3 Basic Routing Issue

Works:
http://localhost/ApplicationName/Home/Index
http://localhost/ApplicationName/
http://localhost/ApplicationName/AnotherController/
404 Error:
http://localhost/ApplicationName/Home
I have an Index view for my Home controller and an Index action.
Everything was working fine, then I refactored and changed the application name.
Now only the Home controller won't default to the Index view when the action is left out
Here's my RegisterRoutes in Global.asax:
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 }
);
}
There was an empty folder in the application root named Home. The application was going there first for the view. Deleted it, problem solved.

ASP.NET MVC 3 basic routing question

I am using ASP.NET MVC 3 and following the tutorial here http://www.asp.net/mvc/tutorials/asp-net-mvc-routing-overview-cs.
I am working on the sign up functionality and trying to make use of routing. So the typical scenario is:
When the user wants to sign up, he would get taken to /Account/SignUp.
Upon succesful sign up, he then gets redirected to /Account/SignUp/Successful.
I thought it would be simple enough but the "Successful" parameter never gets passed in the SignUp method in the controller.
public ActionResult SignUp(string msg)
{
// Do some checks on whether msg is empty or not and then redirect to appropriate view
}
In global.aspx.cs I've got pretty much the vanilla routing:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional });
What am I failing to grasp here?
Your route parameter is called id, so:
public ActionResult SignUp(string id)
{
...
}
or change it to msg if you want:
"{controller}/{action}/{msg}"
Change the parameter from your method to id and create an get method for the /Account/SignUp action
public ActionResult SignUp()
{
//this is the initial SignUp method
}
[HttpPost]
public ActionResult SignUp(string id)
{
//User will be redirected to this method
}

Resources