I'm getting the error
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: /ClientEdit/ClientEdit/1104
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272
when I add HttpPost attribute to a controller. I've looked into this and corrected my code with posted(no pun intended) answers but nothing I've tried works. Here's my controller header:
[HttpPost]
public ActionResult ClientEdit(int id,FormCollection formCollection)
I added HttpPost so I can populate my FormCollection object. As with other SO posts, this causes the error. Removing it solves the issue but my FormCollection doesn't populate any key/value pairs.
My view has its form tag's method set to POST which solved other dev's issues but it doesn't solve mine. I tried adding 'name=' properties to my textbox controls as well as 'id=' but that doesn't work either.
<body>
<form method="post" action="1104" id="form1">
I don't know what else to try.
You need to provide you view that renders the <form> as well. The action on what you show as your HTML looks wrong to me. I would think it would be something like action="/Controller/ClientEdit".
you have given wrong value in the attribute action="1104".
Either you can specify proper route in action attribute or leave it if the route is same as of get.
This should work if GET and POST route are same
<form method="post" id="form1">
.....
</form>
use this:
#using(Html.BeginForm()){
<!--Your form field-->
}
Basically what is happening is that your action attribute is just pointing to the Id you have and not the url to post to.
You can do it manually like this:
<form action="ClientEdit/ClientEdit/1104>
<!--form fields-->
</form>
Yes, you are all correct. I was passing a userid as the action. This was legacy code that 'worked' for another feature but doesn't conform to the MVC pattern. I did some other research (as I'm not too familiar with MVC either) and started again from scratch using MVC as it should be. Thanks everyone.
Related
I am facing a very weird problem that I am unable to solve.
I have this button onclick of which I go to the controller action method where I need to use [Authorize], when I use [Authorize] I get the following error.
The following sections have been defined but have not been rendered for the layout page "~/Views/Shared/_Layout.cshtml": "Scripts".
System.Web.HttpException
However, when I don't use [Authorize] it works fine. Why does it behave like that? Any help would be highly appreciated.
[Authorize]
public async Task<ActionResult> Calendar(int id, string start, string end)
{
//code
}
On your ~/Views/Shared/_Layout.cshtml page you have likely a required section that has to be included on every view, it will be something like this:
#RenderSection("scripts")
You need to change it to this:
#RenderSection("scripts", required: false)
Which means the section will be optional on pages using your layout page.
Old but still relevant:
https://weblogs.asp.net/scottgu/asp-net-mvc-3-layouts-and-sections-with-razor
I've been working on a relatively simple application in ASP.NET Core that displays the status of various nodes in a network. It displays their latest status, and some other information.
For this, I've made a controller which has an action that takes a node's name, performs a lookup in the node manager, and returns the detail view for that specific node. This controller action is implemented like so:
public IActionResult Detail(string name)
{
// Maybe redirect this to the node detail overview once it's done?
if (string.IsNullOrEmpty(name))
return View("PageNotFound");
var viewModel =
Current.NodeManager.Statuses.FirstOrDefault(s => s.Node.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
if (viewModel == null)
return View("PageNotFound");
return View(viewModel);
}
I'm linking to this action from my overview page. The HTML I'm using to do this is:
<a asp-area="" asp-controller="Node" asp-action="Detail" asp-route-id="#status.Node.Name">#status.Node.DisplayName</a>
For one of my nodes, this produces the following link:
http://example.com/Node/Detail/Temeria
And from what I understand from the ASP.NET Core documentation, the controller should capture "Temeria" here as the argument for the Detail action of NodeController, but it reliably refuses to do so. name here is always null.
I've also messed around with the routing in Startup.Configure, by adding the following route to the controller:
routes.MapRoute(
name: "Node Detail",
template: "{controller=Node}/{action=Detail}/{name}");
But unfortunately, to no avail. Every time I invoke the action, be it via clicking the link I've outlined above, or visiting the detail action manually by typing in the URL in my browser, ASP.NET Core spits out the following log line:
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Executing action method Aegis.Controllers.NodeController.Detail (Aegis) with arguments () - ModelState is Valid
I'm at a loss here - am I missing something really obvious here, or does ASP.NET Core not work with primitive types and should I resort to the "model binding" I've been seeing in the guides (which seems to be a bit overkill for an action as simple as this one)?
I've seen various other questions similar to this one, but none of them were for ASP.NET Core. I've done similar projects in classic ASP.NET, and never had this issue with that framework.
Any help would be greatly appreciated. Thanks for your time!
The key is in this line:
<a asp-area="" asp-controller="Node" asp-action="Detail" asp-route-id="#status.Node.Name">#status.Node.DisplayName</a>
I've changed asp-route-id to asp-route-name and everything works fine. So the answer is:
<a asp-area="" asp-controller="Node" asp-action="Detail" asp-route-name="#status.Node.Name">#status.Node.DisplayName</a>
I have an existing asp.net mvc 4 solution. It has several controllers/models/views in separate folders and all work fine
with both GET and POST controller methods. I have added a new folder and added its own controllers/models/views.
When I call the GET controller method, from the view it works fine. But the POST controller method
throws
HTTP Error 405.0 - Method not allowed. The page you are looking for cannot be displayed because an
invalid method (HHTP verb) is being used.
The following is my view and controller
<div id="usercreds" class="items">
#using (Html.BeginForm("SaveCustomer", "NewCustomer"))
{
//form control code here
}
[HttpPost]
public ActionResult SaveCustomer(NewCustomerModel newCustomer)
{
//more code here
}
I added FormMethod.Post in the Html.BeginForm method and added modules="IsapiModule" in web.config file
I still get the same error.
The issue was there were two identically named folders within the VisualStudio solution. Because of this it was posting to the wrong URL. I renamed one of the folders and it worked fine.
So, I know WHY this happens but just unsure the best way to go about fixing it.
The application I am using has a ViewModel and binds/maps the properties from a Model.
This VM is then bound to the View and using the #Html.TextBox() helper.
This is an example: The actual text value from the DB is:
"Université de Montréal"
The rendered output on the HTML in the DOM is:
<input class="validate" id="EmployerName" name="EmployerName" type="text" value="Université de Montréal" />
Now, once the page has been loaded, it does an AJAX GET call by serializing the form and sending that across.
At this point, it DOES hit the controller (because I have the [ValidateRequest(false)] attribute applied to that controller action, but I then get the error:
A potentially dangerous Request.QueryString value was detected from the client (employername="Université de Montré...").
At this point, I am stuck as to what to do to fix the problem and let the request process normally.
I know by adding the in the web.config fixes it, I do not want to change it site wide.
any ideas the best thing to do to allow this request to process as normal?
Controller action:
[ValidateInput(false)]
[OutputCache(NoStore = true, Duration = 0)]
public ActionResult GetMoreData(Criteria crit)
{
...
}
So, I managed to fixed this.
When using the MVC TextBoxFor, it automatically encoded the string (which is nice).
Adding the [ValidateInput(false)] attribute to the controller action works to the point where it can call the action method. I then simply did an HtmlDecode ON SAVE. On load, I simply HtmlEncode the string.
Not the best solution if you have a lot of properties being displayed to the user but it works.
I am trying to understand asp.net server implementation and routing in particular. I have tried to search this forum for answers(for instance this link; WebApi 2.0 Routes not matching with query parameters?, but I havent gotten anything to work. I would like to have a form on the client side:
<"form id="search-form" name="search-form" method="get" action="page.html"
onsubmit="return validate();">
<"input id="inputText" name="question" type="text"
placeholder="Search..." class="inputsearch">
<"/form>
This will allow me to send a query (for instance "test") to the server, which would look like "page.html?question=test". My controller is called products (but it seems to me that I do not need to specify it in an action tag on the form, so I was thinking that I need some routing like:
[HttpGet]
[Route("~/api/products?{queryWord}")]
public IHttpActionResult test(string queryWord)
{
Debug.Write(queryWord);
return Ok("<html>hey</html>");
}
That seemed to be valid from the answers in that link, but I get an error message:
The route template cannot start with a '/' or '~' character and it
cannot contain a '?' character. Parameter name: routeTemplate