Sunday, 11 December 2016

C# - Update Querystring Values in a Url

We will look into a solution to update querystring values in a Url.

using System.Web;

// url has the complete url with querystring 
// ex:
var uriBuilder = new UriBuilder(url);

var query = HttpUtility.ParseQueryString(uriBuilder.Query);

// Existing Querystring parameter xyz value will be updated from 20 to 10
query["xyz"] = "10";

// As QS Parameter hello doesn't existed, it will be added.
query["hello"] = "world";

// Set updated querystring to the Url.
uriBuilder.Query = query.ToString();

// New Url:

Remember to import namespace "System.Web"

Thursday, 8 December 2016

ASP.NET MVC C# - Redirect to External Website from Controller Action Method

Refer the below code snippet for redirection from ASP.NET MVC Controller to an external website.
The MVC Controller method "Redirect()", is used for redirection from MVC Controller.

public ActionResult Index()
    return Redirect("");

Redirect() method can be used for redirecting to an internal url as well.
Find the sample code below:
public ActionResult Index()
    return Redirect("/Customer/List");