asp.net mvc2表单发布不断扩展浏览器url

发布于 2024-10-27 10:36:53 字数 908 浏览 1 评论 0原文

我正在尝试 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

帅气尐潴 2024-11-03 10:36:53

您正在为表单操作使用相对 URL。我建议使用 UrlHelper Html 表单助手。这两者都会产生合适的绝对网址。 Razor 语法示例:

 <form action="@Url.Action( "post", "search" )" ...

 @using(Html.BeginForm( "post", "search" ))
   {
    ...
   }

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:

 <form action="@Url.Action( "post", "search" )" ...

or

 @using(Html.BeginForm( "post", "search" ))
   {
    ...
   }
も星光 2024-11-03 10:36:53

您的操作网址开头需要有一个 / 。

为什么不让 ASP.NET 为你处理这个问题并使用:

<% using (Html.BeginForm("Post", "Search")) { %>

<label><% Response.Write(Model.Title); %></label>
<input type="Submit" Value="First" Name="submitButton"/>

<% } %>

这会给你

<form action="/Search/Post" method="post">

You need a / at the beginning of your action url.

Why not allow asp.net to deal with this for you and use:

<% using (Html.BeginForm("Post", "Search")) { %>

<label><% Response.Write(Model.Title); %></label>
<input type="Submit" Value="First" Name="submitButton"/>

<% } %>

Which will give you

<form action="/Search/Post" method="post">
鸩远一方 2024-11-03 10:36:53

您可以尝试将 action 设置为 /Search/Post,但如果您的应用将安装到非根位置,则会中断。一种可靠的方法是使用类似的东西

<form action="<%= Url.Content("~/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

<form action="<%= Url.Content("~/Search/Post") %>" ...
梦中楼上月下 2024-11-03 10:36:53

尝试使用 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 the form 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文