asp.net mvc2表单发布不断扩展浏览器url
我正在尝试 ASP.NET MVC2。我有一个名为 SearchController 的控制器和一个名为 Search 的视图文件夹,其中包含 Search.aspx。 在我的控制器中,我有:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Post()
{
HPSLucene.Models.Arbitrary arb = new HPSLucene.Models.Arbitrary();
arb.Title = "Post received";
return View("Search",arb);
}
在我看来,我有:
<form action="Search/Post" method="post">
<label><% Response.Write(Model.Title); %></label>
<input type="Submit" Value="First" Name="submitButton"/>
</form>
第一次单击按钮时它工作正常,并且浏览器显示 http://localhost:1824/Search/Post。但是,当我第二次单击该按钮时,浏览器 URL 更改为 http://localhost:1824/Search/搜索/发布,我收到 404。我做错了什么?非常感谢。
I am trying out ASP.NET MVC2. I have a controller called SearchController and a view folder called Search containing Search.aspx.
In my controller I have:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Post()
{
HPSLucene.Models.Arbitrary arb = new HPSLucene.Models.Arbitrary();
arb.Title = "Post received";
return View("Search",arb);
}
In my view I have:
<form action="Search/Post" method="post">
<label><% Response.Write(Model.Title); %></label>
<input type="Submit" Value="First" Name="submitButton"/>
</form>
It works fine the first time I click the button, and the browser shows a url of http://localhost:1824/Search/Post. However, when I click the button a second time the browser url changes to http://localhost:1824/Search/Search/Post and I get a 404. What am I doing wrong? Thanks very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在为表单操作使用相对 URL。我建议使用 UrlHelper Html 表单助手。这两者都会产生合适的绝对网址。 Razor 语法示例:
或
You are using a relative url for the form action. I suggest using either the UrlHelper the Html form helper. Both of these will produce suitable absolute urls. Examples with Razor syntax:
or
您的操作网址开头需要有一个 / 。
为什么不让 ASP.NET 为你处理这个问题并使用:
这会给你
You need a / at the beginning of your action url.
Why not allow asp.net to deal with this for you and use:
Which will give you
您可以尝试将
action
设置为/Search/Post
,但如果您的应用将安装到非根位置,则会中断。一种可靠的方法是使用类似的东西You can try setting
action
to/Search/Post
, but that would break if your app will be installed to a non-root location. A reliable way is to use something like尝试使用
BeginForm
HtmlHelper,而不是手动编写form
标记:http://msdn.microsoft.com/en-us/library/dd505244.aspx
听起来浏览器正在将其解释为相对 URL,并且当当前 URL 为在
/Search/
下,那么它就与此相关。 HtmlHelper应该考虑到这一点,但是YMMV。Try using the
BeginForm
HtmlHelper instead of writing theform
tag manually:http://msdn.microsoft.com/en-us/library/dd505244.aspx
It sounds like the browser is interpreting it as a relative URL and, when the current URL is under
/Search/
then it's making it relative to that. The HtmlHelper should account for this, but YMMV.