Url.Action map a wrong link from Route attribute - asp.net-mvc

This is target controller and action:
[RoutePrefix("Editor")]
public class EditorController : Controller
[HttpGet]
[Route("{id:int}")]
public ActionResult Edit(int id)
Map method calling:
#Url.Action("Edit", "Editor", new { id = page.Id})
result:
/Editor?id=1
required result:
/Editor/1

To achieve the result you want you have to use a route name:
[HttpGet]
[Route("{id:int}", Name = "EditorById")]
public ActionResult Edit(int id)
Then in your view you would use Url.RouteUrl instead of Url.Action:
#Url.RouteUrl("EditorById", new { controller = "Editor", Id = 1, action = "Edit" })
Hope this helps,

Have you checked if you have enabled MVC AttributeRoutes?
routes.MapMvcAttributeRoutes();
see http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx

I just faced with same problem. When i fixed the links - editing is broken (form always redirects to the same page).
Here is solution:
A link
#Html.ActionLink("Edit my nice object", "Edit", new { id=item.Id })
A form in the view Edit.cshtml (specifying Controller name is necessary!)
#using (Html.BeginForm("EditConfirmed", "AppServers"))
The actions in the controller
public class AppServersController
[Route("edit/{id:int?}")]
public ActionResult Edit(int? id)
{
// bla-bla
}
[Route("edit_confirmed")]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EditConfirmed([Bind(Exclude = "Created,LastModified")] AppServerVM appServer)
{
if (!ModelState.IsValid) return View("Edit", appServer);
// bla-bla
}
}
Now both links and editing works.

Related

Default controller and default action in MVC5

I have a website developed in MVC 5, I'm using route attributes for routing.
I've set the default controller and the default action for each controller using the following code
public class CompanyController : MainController
{
[Route("~/", Name = "default")]
[Route("Company/Index")]
public ActionResult Index(string filter = null)
{
//My code here
}
[Route("Company/Edit")]
public ActionResult Edit(int id)
{
//My code here
}
}
I've another controller with a default action :
[RoutePrefix("Analyst")]
[Route("{action=Index}")]
public class AnalystController : MainController
{
[Route("Analyst/Index")]
public ActionResult Index(string filter = null)
{
//My code here
}
[Route("Analyst/Edit")]
public ActionResult Edit(int id)
{
//My code here
}
}
The default controller worked perfectly, but when I navigate to the analyst controller without specifying the name of the action I get the following error:
Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL.
The request has found the following matching controller types:
SurveyWebsite.Controllers.AnalystController
SurveyWebsite.Controllers.CompanyController
How can I correct navigate to http://localhost:61534/analyst and reach the default action ( index) ? The action also should remain accessible by http://localhost:61534/analyst/Index
Thanks for your help.
Give an empty string as the route value for index action so that it works for Analyst, which is your controller route prefix. You can decorate with a second Route attribute for it to work with "Analyst/Index" url where you will pass "Index" to it.
[RoutePrefix("Analyst")]
public class AnalystController : MainController
{
[Route("")]
[Route("Index")]
public ActionResult Index(string filter = null)
{
//My code here
}
[Route("Edit/{id}")]
public ActionResult Edit(int id)
{
//My code here
}
}
This will work for both /Analyst and /Analyst/Index

MVC 3 navigate to correct ActionResult

In my MVC3 project, I have one controller "MyDetailsController" in the
Areas -> Test -> Controller Folder
And in From the ActionResult "Create" in MyDetailsController, I want to call the ActionResult "Edit" of "DetailsController" Which is located in the Controller folder of my application
This is the code I tried
public ActionResult Create()
{
//Some Code
return RedirectToAction("Edit", "Details", new { id = Party.PartyID });
}
But its not loading the exact ActionResult I need.
The the URL am getting is http://localhost:53970/Test/Details/Edit/977612
The URL I needed is http://localhost:53970/Details/Edit/977612
Any help will be appreciated, Thank you.
This will work :
return RedirectToAction("Edit", "DetailsController", new { id = Party.PartyID ,area = ""});

Asp.net mvc Redirect to same page after login

I have a partial view which contains inputs for logging in. The partial view is present on every page of the website so that a user can login to the website from any page.
I have a base controller which is then inherited by all other controllers as below.
When the logon info is submitted, it goes to the logon action in the base controller.
How do I return the view that the logon info was submitted from?
public class BaseController : Controller
{
[HttpPost]
public ActionResult logon(string tx_username, string tx_password)
{
//Verify login details
}
}
public class HomeController : BaseController
{
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
return View();
}
public ActionResult Contact()
{
return View();
}
}
Add another parameter ReturnUrl in the #Html.BeginForm which can be populated using the ViewConext.Controller.ValueProvider, and post the current action and controller name on which user is in the login post action like:
View:
First Way:
#using (Html.BeginForm("logon",
"Home",
FormMethod.Post,
new { ReturnUrl = Url.Action(#ViewContext.Controller.ValueProvider.GetValue("action").RawValue.ToString(),
#ViewContext.Controller.ValueProvider.GetValue("controller").RawValue.ToString()) }))
{
}
2nd Way:
#using (Html.BeginForm("logon",
"Home",
FormMethod.Post,
new { ReturnUrl = this.Request.RawUrl }))
{
}
Action:
[HttpPost]
public ActionResult logon(string tx_username, string tx_password,string ReturnUrl)
{
//If login successful
return Redirect(ReturnUrl);
}

ASP.NET MVC: routing help

Consider two methods on the controller CustomerController.cs:
//URL to be http://mysite/Customer/
public ActionResult Index()
{
return View("ListCustomers");
}
//URL to be http://mysite/Customer/8
public ActionResult View(int id)
{
return View("ViewCustomer");
}
How would you setup your routes to accommodate this requirement?
How would you use Html.ActionLink when creating a link to the View page?
In global.asax.cs, add following (suppose you use the default mvc visual studio template)
Route.MapRoute("Customer",
"Customer/{id}",
new { Controller = "CustomerController", action="View", id="" });
Make sure you put this route before the default route in the template
You then need to modify your controller. For the view,
public ActionResult View(int? id)
{
if (id == null)
{
return RedirectToAction("Index"); // So that it will list all the customer
}
//...The rest follows
}
For your second question, ActionLink is simple.
Html.ActionLink("Link Text", "View", "Customer", new {id=1}, null);

two controllers with same params

i have 2 actions
public ActionResult FilesAdd(int id)
{
FillParentMenuDDL(id);
return View();
}
[HttpPost]
public ActionResult FilesAdd(int id)
{
//some logic...
FillParentMenuDDL(id);
return View();
}
but it is error because of same parameters, but i need only one parameter. first i call page /action/id and then i submit it for example with id and uploaded file, but i access to file using request.files[0]. so what the solution with controllers and same parameters? i see only declare FilesAdd(int? id) in one controller
.Net MVC has an ActionNameAttribute for this purpose. Rename your second action to something like FilesAddPost and then use ActionNameAttribute("FilesAdd")
public ActionResult FilesAdd(int id)
{
FillParentMenuDDL(id);
return View();
}
[HttpPost]
[ActionName("FilesAdd")]
public ActionResult FilesAddPost(int id)
{
//some logic...
FillParentMenuDDL(id);
return View();
}
Add an (unused) form parameter to the POST action. That will make the method signatures different.
[HttpPost]
public ActionResult FilesAdd(int id, FormCollection form)
{
//some logic...
FillParentMenuDDL(id);
return View();
}
You can control the action of the submitted form, it doesn't have to go to the same action.
// Works under MVC 2.0
<% using (Html.BeginForm("action", "controller", FormMethod.Post)) { %>
// code
<% } %>

Resources