MVC5 Change controller name only without effect the actions inside - model-view-controller

I need to change the name of the controller in MVC 5 I used to do the following:
[RouteArea("Dispatch")]
[RoutePrefix("TrackedAssets")]
[Route("{action=index}")]
public class TrackedItemsController : MainControllerBase
{
When i try to hit Index action its OK.
But there are some actions for CRUD, when i tried to hit them by the new URL /Dispatch/TrackedAssets/Edit/47
It gives me:
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: /Lynx/Dispatch/TrackedAssets/Edit/47
Below is the signature of the action:
public ActionResult Edit(int? id)
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit

Your requested route i.e /Lynx/Dispatch/TrackedAssets/Edit/47 will not hit any action method since you have not prefixed your Edit action method with route attribute just like you have set route attribute for index method.
[Route("{action=edit}")]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(int? id)
Try this and please mark this as answer if it helped you.

Related

Cannot view my new MVC 4 application

I just created a new basic MVC 4 application in VS 2010. I just clicked the play button to test it came up in the web browser and I'm getting the following page:
I think I need to change my virtual path to something but I don't know what.
EDIT: Can't see what it says properly in the picture:
Server Error in '/' Application.
--------------------------------------------------------------------------------
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: /
If you created an Empty Project you will need to create a HomeController with an Index Action. You will also need to create a View in ~/Views/Home/ called Index.
The other project templates create this for you but the Empty Project does not.
public class HomeController : Controller
{
public ActionResult Index()
{
return View()
}
}
No one person gave me the full answer. So this is an amalgamation of #MattiVirkkunen and #BrettAlfred
Add this within RouteConfig.cs
routes.MapRoute(
name: "Default",
url: "",
defaults: new { controller = "Home", action = "Home" }
);
Add this within HomeController.cs
public ActionResult Login()
{
return View();
}
Kya Neeta MVC me neyi ho Kya?? I am too :)
I think u have created a start-up page in your applicaition.
Type Http: //localhost:8080/Home/Index in your url
http: //localhost:/ControllerName/ActionName
if that does not work please create a new MVC application from scratch.

Why does RedirectToAction have no effect

When the method with the barcode parameter is called, RedirectToAction has absolutely no effect. I can see that it does indeed return to that route in the URL, but the model is not refreshed and it displays stale data.
In the SQL Server profiler I can see that the call isn't being made to pull back the new data.
How can I force this to happen?
Incidently, the call to: public ActionResult SRScanItem(string barcode) is itself the result of a RedirectToAction from another controller.
I have handful of calls to RedirectToAction("SRPickCollectionItems") elsewhere in the same controller and these all work fine.
Any idea what may be causing this?
public ActionResult SRPickCollectionItems()
{
IEnumerable<ISRPickingItemSummary> items =
SRPickingItemsViewModel.
GetDisplayableChunk(ApplicationState.CollectionId.ToString(),
ApplicationState.AssistantNumber);
return View(items);
}
public ActionResult SRScanItem(string barcode)
{
DataLayer.Instance.AddStockroomFoundItem(barcode, ApplicationState.CollectionId, ApplicationState.AssistantNumber);
return RedirectToAction("SRPickCollectionItems");
}
Maybe the redirect is going to a cached page.
Could you try and decorate SRPickCollectionItems with
[OutputCache(Duration = 0)]
You don't seem to returning to a post method, only a get so it is not obvious how the method with the Redirect is getting called unless you are send the barcode as a query string parameter in a get call through an actionlink or link tag.
Normally you would have two methods named SRPickCollectionItems. The Get method (which you already have) loads the view and the post method (that you are missing) processes the postback. The post method would be thus...
[HttpPost]
public ActionResult SRPickCollectionItemsstring barcode){
DataLayer.Instance.AddStockroomFoundItem(barcode, ApplicationState.CollectionId, ApplicationState.AssistantNumber);
return RedirectToAction("SRPickCollectionItems");
}

ASP.NET MVC 3 won't recognise .cshtml view files

I have ported an mvc 3 app from vs 2010 to vs2012.
The ported app is using .NET 4.
All the old bits work, but with a new view, created in vs 2012, the view engine is not looking for .cshtml files for the view.
For example, when the user requests the index action on the Welcome controller in the Solicitors area, the url is:
mysite.com/solicitors/welcome/gg
(where gg is the user name). In that case, the error that comes back is:
The view 'Index' or its master was not found or no view engine
supports the searched locations. The following locations were
searched: ~/Areas/Solicitors/Views/Welcome/Index.aspx
~/Areas/Solicitors/Views/Welcome/Index.ascx
~/Areas/Solicitors/Views/Shared/Index.aspx
~/Areas/Solicitors/Views/Shared/Index.ascx ~/Views/Welcome/Index.aspx
~/Views/Welcome/Index.ascx ~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx ~/Areas/Solicitors/Views/Welcome/gg.master
~/Areas/Solicitors/Views/Shared/gg.master ~/Views/Welcome/gg.master
~/Views/Shared/gg.master ~/Areas/Solicitors/Views/Welcome/gg.cshtml
~/Areas/Solicitors/Views/Welcome/gg.vbhtml
~/Areas/Solicitors/Views/Shared/gg.cshtml
~/Areas/Solicitors/Views/Shared/gg.vbhtml ~/Views/Welcome/gg.cshtml
~/Views/Welcome/gg.vbhtml ~/Views/Shared/gg.cshtml
~/Views/Shared/gg.vbhtml
I have already added the following key to appsettings in web.config, but it makes no difference.
<add key="webpages:Version" value="1.0" />
EDIT:
Route in SolictorAreaRegistration.cs:
context.MapRoute(
"Solicitors_Welcome",
"Solicitors/Welcome/{nameUser}",
new { controller = "Welcome", action = "Index", nameUser = UrlParameter.Optional }
);
EDIT 2:
Using RouteDebug, I can see that the correct controller and action are found.
Route Data
Key Value
nameUser: gg
controller: Welcome
action: Index
Data Tokens
Key Value
Namespaces: System.String[]
area: Solicitors
UseNamespaceFallback: False
EDIT 3:
The route is found correctly, as I can see from debugging: the Index action is hit.
The problem happens when the line call the view is called:
namespace MyApp.Areas.Solicitors.Controllers
{
[Authorize]
public partial class WelcomeController : Controller
{
//
// GET: /Solicitors/Welcome/
public virtual ActionResult Index(string nameUser)
{
return View("Index", nameUser);
}
}
}
OK, got to the bottom of it:
The Problem:
The problem is that the model of my view is of type string. In my action, I was passing in a string as the model parameter:
public virtual ActionResult Index(string nameUser)
{
return View("Index", nameUser);
}
This will clash with one of the overloads of Controller.View(...):
View(string, string)
The second parameter expects the name of a layout file. When you do this, MVC goes off looking for a layout file with a name of the value of your string, which could be, for example:
"Hello, World. I'm an idiot, but if you give me a decent error message, I might be able to fix the bug."
Obviously, a layout file with that name doesn't exist. Nor does a layout file called "gg" either (my (test) solicitor's username).
The Solution:
The solution is simple:
Specify that the second parameter is the model, not the layout.
public virtual ActionResult Index(string nameUser)
{
return View("Index", model: nameUser);
}
Useful Article:
To view an extended discussion of this very issue, see the following article:
MVC Gotcha: Beware when using your view's model is a string
Many thanks to heartysoft.com for the enlightenment.
It is looking as you can see from the error message:
~/Areas/Solicitors/Views/Welcome/gg.cshtml
If you need to look for the Index view then you need to specify it:
http://mysite.com/solicitors/welcome/index/gg

REST Routes & Verbs - Handling routes without a parameter

Background
I wrote an REST-like API service controller for a simple Blog app. I use two routes to handle basic CRUD:
// Actions should handle: GET, POST, PUT, DELETE
routes.MapRoute("Api-SingleThing", "thing/{id}",
new { controller = "ThingService", action = "SingleThing" });
// Action should handle: GET
routes.MapRoute("Api-AllThings", "things",
new { controller = "ThingService", action = "AllThings" });
The matching controller code looks like:
[HttpGet]
public ActionResult AllThings() {}
[HttpGet]
[ActionName("SingleThing")]
public ActionResult Get(string id) {}
[HttpPost]
[ActionName("SingleThing")]
public JsonResult Create(Thing thing) {}
[HttpPut]
[ActionName("SingleThing")]
public ActionResult Update(Thing thing) {}
[HttpDelete]
[ActionName("SingleThing")]
public ActionResult Delete(int id) {}
The [ActionName()] attribute is used to avoid route constraints, so that the route when triggered always calls the "SingleThing" action on the controller - regardless of the HTTP verb. This lets the controller action methods that share the name, decide who handles the request based on the [HttpVerb] attributes.
In my blog app, this works like a charm, but only because the {id} route parameter (aka. the slug) is always present, even on POST and PUT requests.
With this new API shown above, the POST and PUT actions do not trigger the top route (ex. no {id} value), and when they trigger the second route, there is no method to handle them because of the verbs.
Question
What's the best way to maintain this REST-ful URL architecture and verb handling, and ensure I trigger my POST and PUT actions?

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