How asp.net MVC returns the original page after performing a task __.net

Source: Internet
Author: User
Tags urlencode

Many times after the deletion of an element, we all want to return to the original page, how to achieve it.

Suppose in the index.cshtml (or aspx) of Xcontroller:

<a>


Delete </a>, in order for the deletion to return to the index page, you can use:

<a>


And in the XController.cs, the original

[HttpPost]
ActionResult Delete (int id)
{
    try
    {
        ...
        Return Redirecttoaction (...);
    }
    Catch
    {
    }
}

To

[HttpPost]

ActionResult Delete (int id, string backurl)
{
    try
    {
        ...
        ) Return Redirect (Backurl);
    }
    Catch
    {
    }
}

Request.url contains all the address information (including parameters), which is a URI type instead of string, so many additional operations can be performed (such as as long as address/server/query parameters, etc.), and the details can be searched online.

Never tried "/area/controller/action/id" how to do, should be roughly similar.

2011-08-18 added: When there is a "&" in the Backurl (where there are multiple parameters to return), this will be a direct error , you must:

<a>

In order to encode the return code (all of the special characters into% or whatever, Chinese also run).

If you write more, you'll find trouble as a helper:

    public static partial class Helper
    {public
        static string Returnurlof (this HtmlHelper htmlhelper, Httprequestbase request)
        {return
            httputility.urlencode (request). Url.tostring ());
        }
    


Write the Html.returnurlof (request) in the place where you originally wanted to write those things (request is a property of view and is available as HTML).
But not the laziest is only more lazy, if you and I have the same <a> encapsulation, then the interface can be encapsulated directly into:

@Html. Link ("x", "/sfc/categories/delete?rootid=" + root.id + "&id=" + model.id, Returnrequest:request)

All encode and request issues are handled inside link, which is the ultimate practice.

Download the Free Agile Development textbook: "The Martian Agile Development Handbook"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.