Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 如何将所有操作映射到C#类而不是ASP?_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

Asp.net mvc 如何将所有操作映射到C#类而不是ASP?

Asp.net mvc 如何将所有操作映射到C#类而不是ASP?,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,我想映射所有URL,如/Home/User或/Home/About或/Home/Register或。。。到c#页面,如下所示: public class User { public string run(UrlParameter id){ return "Hello World"; } } public class ProductsController : ApiController { Product[] products = new Product

我想映射所有URL,如/Home/User或/Home/About或/Home/Register或。。。到c#页面,如下所示:

public class User
{
    public string run(UrlParameter id){
        return "Hello World";
    }
}
public class ProductsController : ApiController
{

    Product[] products = new Product[] 
    { 
        new Product { Id = 1, Name = "Tomato Soup", 
                      Category = "Groceries", Price = 1 }, 
        new Product { Id = 2, Name = "Yo-yo", 
                      Category = "Toys", Price = 3.75M }, 
        new Product { Id = 3, Name = "Hammer", 
                      Category = "Hardware", Price = 16.99M } 
    };

    public IEnumerable<Product> GetAllProducts()
    {
        return products;
    }

    public Product GetProductById(int id)
    {
        var product = products.FirstOrDefault((p) => p.Id == id);
        if (product == null)
        {
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }
        return product;
    }
} 
例如,User.cs页面如下所示:

public class User
{
    public string run(UrlParameter id){
        return "Hello World";
    }
}
public class ProductsController : ApiController
{

    Product[] products = new Product[] 
    { 
        new Product { Id = 1, Name = "Tomato Soup", 
                      Category = "Groceries", Price = 1 }, 
        new Product { Id = 2, Name = "Yo-yo", 
                      Category = "Toys", Price = 3.75M }, 
        new Product { Id = 3, Name = "Hammer", 
                      Category = "Hardware", Price = 16.99M } 
    };

    public IEnumerable<Product> GetAllProducts()
    {
        return products;
    }

    public Product GetProductById(int id)
    {
        var product = products.FirstOrDefault((p) => p.Id == id);
        if (product == null)
        {
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }
        return product;
    }
} 
当用户发送/Home/user.请求时,我希望。。调用User类的Run函数并向用户显示返回值。如何在ASP MVC中做到这一点

我可以在RouteConfig中更改路线吗?现在我的MVC路线是:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}
当我调用某个url程序时,在c#net中的MVC项目的默认视图文件夹中运行一个asp页面

欲了解更多解释:

我的客户端程序和服务器端程序之间有一个协议,即JSON。当客户端请求某些内容时,我希望返回字符串JSON,若要执行此操作,我不需要asp页面来呈现html,我只需要调用一些函数将JSON返回给客户端


我怎样才能用MVC做到这一点呢?

我假设你的问题有两个部分

第一部分:将url映射到页面。从某种意义上说,这就是路由。它将一个url映射到一个操作,该操作可以是一个页面,也可以是一个资源(如图片),或者是一个响应(如JSON数据)。请注意,它并不总是一个页面,通常一个url映射到一个资源

阅读URL路由文档:

在上面的示例中:fakedomain.com/Page1将在
HomeController
类上运行
Page1
方法,如果没有添加任何代码,它将在视图文件夹中搜索
Page1.aspx
Page1.cshtml

在这一点上,我建议阅读关于REST的文章。我建议这篇文章:


第二部分:如何返回JSON数据。那么您使用的是WebApi。见文件

WebApi允许您编写基于请求返回数据的控制器。因此,如果您的客户端发送一个Ajax请求,并将accept头设置为application/json,那么WebApi将返回json

它还遵循asp.NETMVC的控制器、路由和操作的典型系统

因此,要返回表示产品的JSON数据,您需要一个如下所示的ProductController:

public class User
{
    public string run(UrlParameter id){
        return "Hello World";
    }
}
public class ProductsController : ApiController
{

    Product[] products = new Product[] 
    { 
        new Product { Id = 1, Name = "Tomato Soup", 
                      Category = "Groceries", Price = 1 }, 
        new Product { Id = 2, Name = "Yo-yo", 
                      Category = "Toys", Price = 3.75M }, 
        new Product { Id = 3, Name = "Hammer", 
                      Category = "Hardware", Price = 16.99M } 
    };

    public IEnumerable<Product> GetAllProducts()
    {
        return products;
    }

    public Product GetProductById(int id)
    {
        var product = products.FirstOrDefault((p) => p.Id == id);
        if (product == null)
        {
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }
        return product;
    }
} 

我强烈建议您从和处获取所有必要条件,如visual studio和asp.net-mvc。

除非我错过了您的问题,否则我认为您对mvc的工作原理有一个根本性的误解。实际上,我的客户端程序都是用javascript工作的,我希望从我的所有服务器页面获得JSON,它可以提供给我的javascript。。现在我该怎么办?我不需要ASP或CSHTML页面,我只需要返回每个页面中每一个动作的JSON函数。@ MeHdiyEGANEH我会考虑与ASP.NET Web API集成。这就是webapifor@MehdiYeganeh您应该使用VisualStudio2010或更高版本并安装MVC4,然后您应该遵循我在回答中链接到的WebAPI教程。非常感谢gideon和Daniel a.White&Dave a向我介绍WebAPI