【问题标题】:Why does the URL changes to the action name to which I have posted the data?为什么 URL 更改为我已发布数据的操作名称?
【发布时间】:2017-10-27 12:40:30
【问题描述】:

我有这个控制器

  [HttpPost]
        //[Bind("")]
        public ActionResult AddStories(Stories st, HttpPostedFileBase files)
        {
            try
            {
                if (files != null)
                {
                    string filePath = Path.Combine(Server.MapPath("~/UploadedFiles/"), Path.GetFileName(files.FileName));
                    files.SaveAs(filePath);
                }

                st.Image = Path.GetFileName(files.FileName);
                listofStories.Clear();
                listofStories = bo.GetAllImages();

                if (bo.insertImages(st))
                {
                    ViewBag.Data = "Added";

                    ViewBag.Grid = listofStories;

                    ViewBag.Style = "display:none";
                    ViewBag.StyleAdd = "";
                }
                else
                {

                }
            }
            catch (Exception ex)
            {
                ViewBag.Data = ex.Message;
            }


            return View("GetStories", st);
        } 

为此,我有一个嵌入在主视图中的绑定局部视图,GetStories

当我向它提交数据时,URL 会更改为 /Stories/AddStories,但我希望它在将数据发布到 AddStories 后成为父母的视图,例如 /Stories/GetStories。

我尝试了几件事,但没有任何效果。

我很天真。

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4 asp.net-web-api razor


    【解决方案1】:

    当您发帖时,您的浏览器会导航到发帖网址。之后只有两种方法可以让它显示不同的 url:

    • 不是返回视图就好像他们点击了那个 url,而是返回一个 redirect 到首选 url(这样浏览器会先发布一个帖子,然后是一个 get)
    • 使用客户端诡计来更改显示的 url - 例如replaceState (取决于浏览器)

    例如,当您发布此问题时,您的浏览器对/questions/ask/submit 进行了 POST,该路由后面的 C# 的最后一行是重定向到 /questions/46975286/why-does-the-url-changes-to-the-action-name-to-which-i-have-posted-the-data

    【讨论】:

    • @StackyUnderflow 作为记录,post/redirect/get 方法的另一个优点是它避免了大多数浏览器都会出现的“刷新页面将重新提交您的内容”警告。刷新“get”意味着没问题(“get”意味着幂等)
    • 是的,这是真的。我想这也是 AJAX 所做的。
    • 但我猜,Post 确实会发出警告,bcz 直到现在我还在使用 post 并面临重新提交警告
    猜你喜欢
    • 2018-08-15
    • 2013-01-24
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多