Return void in an Actionresult method?

Asked

Viewed 53 times

-3

I have this method below, and I need to have a condition that depending on it will open a new tab with a url:

public ActionResult Index()
{
    //condição
    return Response.Write(@"<script type='text/javascript' language='javascript'>window.open('page.html','_blank').focus();</script>");
}

tried

ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), sb.ToString(), true);

But it always returns that it is not possible to return void in a method ActionResult. And I really need you to open a new tab.

  • Please avoid long discussions in the comments; your talk was moved to the chat - who want to read or proceed, just click on the link

2 answers

3

ActionResult is to use methods for this if you will not use a ActionResult so there’s no need to force it

The Response.Write is not part of the namespace System.Web.Mvc, return of an Actionresult must contain:

  • System.Web.Mvc.ContentResult
  • System.Web.Mvc.EmptyResult
  • System.Web.Mvc.FileResult
  • System.Web.Mvc.HttpStatusCodeResult
  • System.Web.Mvc.JavaScriptResult
  • System.Web.Mvc.JsonResult
  • System.Web.Mvc.RedirectResult
  • System.Web.Mvc.RedirectToRouteResult
  • System.Web.Mvc.ViewResultBase

So if you want to use a "string" as output use Contentresult:

public ContentResult Index()  
{  
    return Content(@"<script type='text/javascript' language='javascript'>window.open('page.html','_blank').focus();</script>"));  
}

Which by default will return as text/html

  • I need Actionresult, because I have a case, where I open a Redirecttoaction, in this specific, I need to open in a new tab a url

  • @Mariana Contentresult is DERIVED from Actionresult, so it should work. ... open in NEW TAB is the responsibility of Javascript after the downloaded page, it does not occur in Backend

  • When I switch to contentResult, it returns me: It is not possible to implicitly convert type "System.Web.Mvc.Redirecttorouteresult" into "System.Web.Mvc.Contentresult"

  • When I use Content, it opens, but the page I was on, it loses the information.

  • 1

    But this redirecttoroueresult is something else and probably another route, without looking at what you did can not understand, probably your routes were configured as redirect q has no sense, because the result is triggered by the front

  • what she intends to do this code is not enough Guilmerme she needs to pass a flag to view and in this view open the page.

Show 1 more comment

-3

maybe something along those lines:

public ActionResult NewWindow()
{
    return Content("<script>window.open('{url}','_blank')</script>");
}
  • It opens in a new tab, but does not open the correct link, it does so http://localhost:2021/%7Bhttps//url, and the current page loses the data

Browser other questions tagged

You are not signed in. Login or sign up in order to post.