Using MVC out of the box I found the generated URLs can be misleading and I wanted to know if this can be fixed or if my approach/understanding is wrong.
使用MVC开箱即用我发现生成的URL可能会产生误导,我想知道这是否可以解决,或者我的方法/理解是否错误。
Suppose I have a CreateEgg page, which has a form on it, and once the form is filled in and submitted the user is taken to a ListEggs page with the new egg in it.
假设我有一个CreateEgg页面,其上有一个表单,一旦表单被填入并提交,用户就会被带到ListEggs页面,其中包含新的egg。
So my egg controller will look some thing like this:
所以我的蛋控制器会看起来像这样:
public class EggController : Controller
{
public void Add()
{
//do stuff
RenderView("CreateEgg", viewData);
}
public void Create()
{
//do stuff
RenderView("ListEggs", viewData);
}
}
So my first page will have a url of something like http://localhost/egg/add and the form on the page will have an action of:
所以我的第一页将有一个类似http:// localhost / egg / add的URL,页面上的表单将有以下动作:
using (Html.Form<EggController>(c => c.Create())
Meaning the second page will have a url of http://localhost/Egg/Create, to me this is misleading, the action should be called Create, because im creating the egg, but a list view is being displayed so the url of http://localhost/Egg/List would make more scene. How do I achieve this without making my view or action names misleading?
意思是第二页会有一个http:// localhost / Egg / Create的网址,对我来说这是误导,动作应该叫做Create,因为im创建了egg,但是列表视图正在显示所以http的url :// localhost / Egg / List会产生更多场景。如何在不使我的观点或行动名称误导的情况下实现这一目标?
3 个解决方案
#1
4
The problem is your action does two things, violating the Single Responsibility Principle.
问题是你的行为做了两件事,违反了单一责任原则。
If your Create action redirects to the List action when it's done creating the item, then this problem disappears.
如果您的“创建”操作在创建项目时重定向到“列表”操作,则此问题将消失。
#3
0
ActionVerbs Outlined in Scott Gu's post seem to be a good approch;
ActionVerbs在Scott Gu的帖子中概述了似乎是一个很好的approch;
Scott says:
You can create overloaded implementations of action methods, and use a new [AcceptVerbs] attribute to have ASP.NET MVC filter how they are dispatched. For example, below we can declare two Create action methods - one that will be called in GET scenarios, and one that will be called in POST scenarios
您可以创建操作方法的重载实现,并使用新的[AcceptVerbs]属性让ASP.NET MVC过滤它们的分派方式。例如,下面我们可以声明两个Create动作方法 - 一个将在GET场景中调用,另一个将在POST场景中调用
[AcceptVerbs("GET")]
public object Create() {}
[AcceptVerbs("POST")]
public object Create(string productName, Decimal unitPrice) {}
#1
4
The problem is your action does two things, violating the Single Responsibility Principle.
问题是你的行为做了两件事,违反了单一责任原则。
If your Create action redirects to the List action when it's done creating the item, then this problem disappears.
如果您的“创建”操作在创建项目时重定向到“列表”操作,则此问题将消失。
#2
#3
0
ActionVerbs Outlined in Scott Gu's post seem to be a good approch;
ActionVerbs在Scott Gu的帖子中概述了似乎是一个很好的approch;
Scott says:
You can create overloaded implementations of action methods, and use a new [AcceptVerbs] attribute to have ASP.NET MVC filter how they are dispatched. For example, below we can declare two Create action methods - one that will be called in GET scenarios, and one that will be called in POST scenarios
您可以创建操作方法的重载实现,并使用新的[AcceptVerbs]属性让ASP.NET MVC过滤它们的分派方式。例如,下面我们可以声明两个Create动作方法 - 一个将在GET场景中调用,另一个将在POST场景中调用
[AcceptVerbs("GET")]
public object Create() {}
[AcceptVerbs("POST")]
public object Create(string productName, Decimal unitPrice) {}