重定向到URL - asp.net vs MVC

时间:2013-10-22 19:56:43

标签: c# javascript asp.net asp.net-mvc asp.net-mvc-3

我的问题有点类似 How to get a response "stream" from an action in MVC3/Razor?

但我尝试过他们的方法但没有成功。

详情

我正在使用MVC3,.net4,c#,

javascript和第三方组件打开文件。

我有viewStuff.jsViewFile.aspx相关联。

viewStuff.js我有

var component = 'code to initialize'

PREVIOUSLY

我曾经将aspx页面与此javascript相关联,并且效果很好

viewStuff.js我有

component.openFile("http://localhost:8080/ViewFile.aspx");

重定向到aspx页面

ViewFile.aspx.cs文件以HTTPResponse

的形式返回与文件相关的数据
  protected void Page_Load(object sender, EventArgs e)
        {
            this.Response.Clear();

            string stuff = "abcd";
            this.Response.Write(stuff);

            this.Response.End();
        }

NOW

我想要做的就是将aspx替换为Controller,这将返回相同的内容。

viewStuff.js我有

component.openFile("http://localhost:8080/ViewFile/Index");

Controller看起来像

public class ViewFileController: Controller{

   public ActionResult Index()
   {
     string stuff = "abcd";
     return stuff;
   }
}

我唯一的问题是我的component.openFile()方法无法使用MVC URL访问Controller。  Index()启动后我就会有活动断点,但从不这样做  受到打击。

我不知道是否是它     - URL
    - MVC - URL是方法而不是物理文件

此外,我不确定如果这可能有所帮助,如何弄乱Rou​​teConfig()。

编辑:路由配置: -

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }                    
            );

(如果需要,我可以获得更多详细信息。在downvoting之前让我知道)

1 个答案:

答案 0 :(得分:2)

有两种可能性可以想到

  1. 从控制器操作中返回ContentResult

    public class ViewFileController : Controller
    {
       public ActionResult Index()
       {
           string stuff = "abcd";
           return Content(stuff);
       }
    }
    
  2. 使用视图:

    public class ViewFileController : Controller
    {
       public ActionResult Index()
       {
           return View();
       }
    }
    

    并在相应的Index.cshtml视图中,您可以放置​​您想要的任何标记。

  3. 此外,在您的控制器中放置任何断点之前,请在浏览器地址栏中打开http://localhost:8080/ViewFile/Index网址,看看它是否返回正确和预期的数据。