Action Name not showing Up In URL - asp.net-mvc

I am trying to create a form in a view and am running into trouble with the routing; it seems to be totally ignoring the action name that I am providing. Here are my controller methods (Controller is called "MyController"):
[HttpGet]
public ActionResult CreateNewRequest()
{
if (ModelState.IsValid)
{
MyViewModelClass model = new MyViewModelClass();
return View("CreateNewRequest", model);
}
else
{
return DisplayModelStateError(ModelState.Values);
}
}
[HttpPost]
public ActionResult CreateNewRequest(MyViewModelClass model)
{
//code to process the view model
return RedirectToAction("Index");
}
In my view, I have tried several variations of the same thing to render a form, including:
#using (Html.BeginForm("CreateNewRequest","My",FormMethod.Post))
#using (Html.BeginForm("CreateNewRequest","My"))
#using (Html.BeginForm("CreateNewRequest","My",null,FormMethod.Post))
They all fail when the user clicks the submit button; I get an error that says:
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: /My
What is especially odd is that if i reverse them (which I know to be incorrect), I get an error message implying it looked for the action as opposed to totally ignoring it. For example, if I try
#using (Html.BeginForm("My","CreateNewRequest",FormMethod.Post))
I get the following error message:
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: /CreateNewRequest/My
Any ideas here?

Related

MVC 5, HttpGet works, HttpPost does not work, why?

I am making some custom methods in an MVC 5/Web Api. I try to make a custom GET method as follows:
[AllowAnonymous]
[Route("TrainItemCheckIfWorks")]
[HttpGet]
public string TrainItemCheckIfWorks()
{
return "Hello";
}
Which works beautifully. I do the same thing with HttpPost, as follows:
[Route("CheckPost")]
[HttpPost]
[AllowAnonymous]
public double[] GetPostItem()
{
return new double[] {1, 2, 3};
}
The HttpPost fails but I do not get a specfic error. I just get 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: /TrainItemModels/CheckPost
THis is very frustrating and I have been spending all weekend trying to figure out why I am getting this error. Any help will greatly help

RediretAction in MVC causes "The Web server is configured to not list the contents of this directory"

I have a contentController like this :
public class ContentController : Controller
{
public ActionResult Index()
{
var model = obj.GetContentlist();
return View(model);
}
[HttpPost]
public ActionResult Create(Content content)
{
obj.AddNewContent(content);
obj.Save();
return RedirectToAction("Index","content");
}
}
That as you can see contains create action .But in the last line ,when it is going to be executed RedirectToAction("Index","content"); i got this error :
HTTP Error 403.14 - Forbidden
The Web server is configured to not list the contents of this directory.
Most likely causes:
A default document is not configured for the requested URL, and directory browsing is not enabled on the server.
The default MVC template includes a "Content" folder. Since you're redirecting to content/index and index is most likely the default action, you actually get redirected to /content -- which is the content directory rather than your controller. If I were you I would choose another name for your controller to avoid the conflict.

Returning a GET view after a POST view Fails ASP.NET MVC

I have a View in MVC called Action, which accepts and ID as parameter. I also have another view called Action, but it is marked as HTTPPOST.
The POST version of Action can fail programatically. When it fails, I want to add a ModelError, and then Redirect to the GET version of Action, whilst I provide the ID .
In short => I want to transfer to a GET view from a POST view, and pass parameters. Something along the lines of
ModelState.AddModelError("", "Could not verify token");
return RedirectToAction("Action", "ExpiredAccounts" new { aid = myId });
Where Action is the View, ExpiredAccounts is the Controller and AID is the Account ID. This of course does not work since you can add a model error to a view, not redirecting
Thanks
You'd better return the same view in this case instead of redirecting:
ModelState.AddModelError("", "Could not verify token");
var model = repository.Get(myId);
return View(model);
The correct flow of the Redirect-After-Post pattern is the following:
GET request -> some form is displayed
POST request -> the form is submitted to the server. Two possible cases here:
Validation succeeds => Redirect.
Validation fails => redisplay the same view so that the user can fix the errors
If you want to violate this best practice you could always send the error message as query string parameter when redirecting:
return RedirectToAction(
"Action",
"ExpiredAccounts"
new { aid = myId, error = "Could not verify token" }
);
and then in side the target action verify whether this parameter has been supplied and add an error to the modelstate:
public ActionResult Action(int myId, string error)
{
if (!string.IsNullOrEmpty(error))
{
ModelState.AddModelError("", error);
}
...
}

How can I set up my default view in MVC4 to point to a specific area, controller and action?

I have a User area and inside this I have the following registered:
context.MapRoute("DefaultRedirect",
"",
new { controller = "Account", action = "Login" }
);
When I use routeDebug it tells me that when I connect to my site www.xxx.com then it will try to call
area = User, controller = Account, action = Login
When I connect directly using: www.xxx.com/User/Account/Login my login page appears.
When I don't use routeDebug and connect to my site www.xxx.com then I get an error message saying:
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: /
Here's my controller action method:
public class AccountController : Controller
{
//
// GET: /Account/Login
[AllowAnonymous]
public ActionResult Login()
{
ViewBag.ReturnUrl = "xx";
return View("~/Areas/User/Views/Account/Login.cshtml");
}
I am very confused as routeDebug appears to show I am going to the right controller and action however when I don't use that and place a breakpoint it does not seem to go to the controller action.
if this controller is inside the same area i think you just can use
[AllowAnonymous]
public ActionResult Login()
{
ViewBag.ReturnUrl = "xx";
return View();
}
Either way if you have only the views on a different areas you can use
return View("~/Views/YourArea/YourController/YourView.aspx");
return RedirectToAction("Login", "Account");
Will redirect to specific controller and specific action.
If account is deeper in folders just include the path

Mvc http post method gives me "The resource cannot be found. "

here is my root controller :
[HttpPost]
public ActionResult Index()
{
}
but it couldn't load project it give me :
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.
Try this
public ActionResult Index()
{
}
[HttpPost, ActionName("Index")]
public ActionResult IndexPost()
{
}
The ActionName attribute enables you to create a method of one name that relates to another

Resources