c#
  • javascript
  • asp.net
  • asp.net-mvc
  • asp.net-mvc-3
  • 2013-10-22 58 views 1 likes 
    1

    我的问题是有点类似 How to get a response "stream" from an action in MVC3/Razor?重定向到URL - asp.net VS MVC

    ,但我已经试过了,没有成功的方法。使用MVC3,.NET4,C#,

    详情

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

    我有一个viewStuff.js连接到ViewFile.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(); 
         } 
    

    窗体现在

    相关的所有文件数据我想要做的是替换这aspxController将返回相同的东西。

    在我viewStuff.js我有

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

    Controller看起来像

    public class ViewFileController: Controller{ 
    
        public ActionResult Index() 
        { 
        string stuff = "abcd"; 
        return stuff; 
        } 
    } 
    

    我唯一的问题是,我的component.openFile()方法是无法使用才能到Controller MVC网址。 一旦Index()开始,我就得到了积极的断点,但他们从来没有 受到打击。

    我不知道它
    - 网址
    - MVC - 事实上,网址是方法,而不是物理文件

    而且,我不确定如何各地的RouteConfig一团糟()如果这可能有帮助。

    编辑:路由配置: -

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

    (如果需要的话,我可以得到更多的细节让我downvoting之前知道。)

    +0

    是否希望viewstuff.js将数据返回到页面或者您是否尝试重定向到另一个页面。不确定我是否在意你的问题,但你可能需要使用JSONResult方法而不是ActionResult – Brian

    +0

    你可以告诉你路由配置吗? – alexmac

    +0

    如果您运行应用程序,然后通过'http:// localhost:8080/ViewFile/Index'进入浏览器地址栏,会发生什么?那么断点是否会被击中?另外,你可能想在你的控制器方法中尝试'返回Content(stuff);'。 –

    回答

    2

    2的可能性浮现在脑海

    1. 返回ContentResult from your controller action:

      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查看,你可以把你想要的任何标记。

    而且把所有的断点在控制器打开你的浏览器地址栏中的URL http://localhost:8080/ViewFile/Index,看它是否返回正确的和预期的数据之前。

    +0

    是的,使用任何浏览器打破断点。这就是为什么我想也许我的第三方组件不喜欢mvc风格的网址,我返回预期的数据,因为我的'上一节'部分亮点 – heyNow

    +0

    'ContentResult'没有帮助。有没有办法在点击控制器之前进入视图? – heyNow

    +0

    不,这违背了MVC模式的全部概念,其中视图由控制器操作呈现。所以通过这个定义,MVC应用程序的入口点就是Controller。 –

    相关问题