2012-01-16 103 views
-1

我傳遞的URL如「http://localhost:6384/Name/4:」但這是一個錯誤。ASP.NET MVC 3應用程序不工作

控制器的方法:

// 
// GET: /Name/5 
     public string SetName(int id) 
     { 
      return "You entered: " + id; 
     } 

錯誤:

Server Error in '/' Application. 

HTTP Error 400 - Bad Request. 

Version Information: ASP.NET Development Server 10.0.0.0 

請幫幫我!

+1

您的網址是/姓名/但除非你已經創建了一個特定的路線這一點,應該是/ SetName /,因爲這是方法名稱。如果這不適用於您,則可以在global.asax中爲/ {action}/{id}添加路由。我不記得語法,但這是一個選項。 – 2012-01-16 11:53:19

回答

0

你應該試試這個:

public ActionResult SetName(int id) { 
     return Content("You entered: " id); 
    } 

對不起,這是比這更容易。您輸入的網址不正確。您尚未指定控制器名稱。

http://localhost:6384/CONTROLLERNAME/SetName/4

+0

謝謝。它也不工作! – 2012-01-16 11:48:14

+0

查看我的更新回答 – KMan 2012-01-16 11:51:05

2

驗證以下步驟,

1)在global.axas驗證默認根如下,

routes.MapRoute(
       "Default", // Route name 
       "{controller}/{action}/{id}", // URL with parameters 
       new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults 
      ); 

2)在url提到控制器名稱,如果控制器和動作如下

public class HomeController : Controller 
    { 
     public ActionResult SetName(int id) 
     { 
      ViewBag.Message = "Welcome to ASP.NET MVC!"; 

      return View(); 
     } 
    } 

然後url會被,

http://localhost:6384/Home/SetName/4 
0

在此錯誤消息。 HTTP狀態400錯誤請求 - 錯誤語法

可能是因爲Action Result找不到或不存在。

這是錯誤的操作方法:

// GET: /Name/5 
public string SetName(int id) 
{ 
    return "You entered: " + id; 
} 

我會糾正你的動作方法:

[HttpGet] 
public ActionResult Setname(int id) 
{ 
    ViewBag.Result = "You entered: " + id; 

    return View(); 
} 
相關問題