-1

I am beginning developer in asp.Net MVC5.
In my MVC project i use with web service that return me a string URL the URL is from another domain.

I want to move to the URL.

For clear myself: The client fill form home page and press submit, in the server side i send a request web service with parameters from the form and get URL with another domain and this URL i need to present as second page to the client

public class HomeController : Controller
{
    public ActionResult Home()
    {
        return View("~/Views/Home/home.cshtml");
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult doSomething(Something obj)
    {
        //use web service and get string URL
        string urlString = ;// get from the web service response.
        return View();// want write in the ();
    }
}
tal
  • 295
  • 1
  • 4
  • 20
  • What do you want to do with the URL that is coming from webservice response. Do you want to navigate to the url – ankur Dec 05 '17 at 12:40
  • There are multiple ways you can do this. You can use v view data or viewbag to start with. You can use model also to send data to the view. – Chetan Dec 05 '17 at 12:41
  • yes that what i want - @ankur – tal Dec 05 '17 at 12:41
  • https://stackoverflow.com/questions/799511/how-to-simulate-server-transfer-in-asp-net-mvc may be this will help – ankur Dec 05 '17 at 12:43
  • You've asked in your question how you print your URL to the View, yet in your comments you clarify that you're wanting to navigate to it. Which one is it? Can you update your question to match, please. – Geoff James Dec 05 '17 at 13:01

2 Answers2

1

Also this is usefull for navigation in MVC.

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult doSomething(Something obj)
{
    //use web service and get string URL
    string urlString = ;// get from the web service response.

    if (!string.IsNullOrEmpty(urlString))
    {
        //if the url is from within the domain.
        return RedirectToAction(urlString);
      //if the url is from other domain use this
      //return Redirect(urlString);
    }

    //If the urlString is empty Return to a error page
    return View("Error");
}
ankur
  • 4,565
  • 14
  • 64
  • 100
0

The url is from another site not the same domain

If you want to redirect to an external URL, you will need to use the Redirect() method.

Like so:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult DoSomething(Something obj)
{
    // Use web service to get the string URL
    string urlString = ...;

    if (string.IsNullOrEmpty(urlString))
    {
        // If the urlString is empty, take the user to an Error View.
        return View("Error");        
    }

    // Redirect the user to the urlString
    return Redirect(urlString);
}

I would recommend also doing some checking to ensure the URL is definitely valid. You can do this using the Uri static method, IsWellFormedUriString() - this returns a bool.

Like so:

if (!Uri.IsWellFormedUriString(urlString, UrlKind.Absolute))
{
    // If the urlString is not a well-formed Uri, take the user to an Error View
    return View("Error");
}

// Redirect the user to the urlString
return Redirect(urlString);

Alternatively, if you're redirecting to an internal Action, use the RedirectToAction() method, as @ankur suggested.


As an extra note: Ensure that your C# method names use PascalCase. Conserve camelCase for local variables/private fields.

So, you would use DoSomething(...), instead of doSomething(...) (I've done this in my example).

Hope this helps.

Geoff James
  • 3,122
  • 1
  • 17
  • 36