简体   繁体   English

MVC:不存在的动作

[英]MVC: actions that don't exist

Ihaven't done much MVC and still learning the hard way on how to do things. Ihaven并没有做太多的MVC,但仍在学习如何做事的艰难方法。

MVC 1 and C# MVC 1和C#

The Problem I want to provide a customer with a link such as www.temp.com/redirects/cust100?id=123&url=www.nothere.com 问题我想为客户提供一个链接,例如www.temp.com/redirects/cust100?id=123&url=www.nothere.com

from the URL i know it will go to the controller of "redirects" but there isn't an Action of "cust100". 从URL,我知道它将转到“重定向”的控制器,但没有“ cust100”的操作。 How do i create an ActionResult(or something else) that will grab the action so i could query it against a DB to check it is valid before rerouting them else where on my site? 我如何创建一个ActionResult(或其他东西)来抓取动作,以便我可以对数据库进行查询以检查其是否有效,然后再将其重新路由到我的网站上的其他位置?

If i have explained myselft too well please feel free to ask more questions. 如果我对自己的解释太好,请随时提出更多问题。

Kind regards, Pete 亲切的问候,皮特

Your route: 您的路线:

routes.MapRoute("Redirects",               
               "{controller}/{cust}",
                new {controller = "redirects", action = "Index", cust = "" });     

this would make your url work by sending the paramaters to the index method as the default action: 通过将参数发送到index方法作为默认操作,这将使您的url起作用:

/redirects/cust100?id=123&url=www.nothere.com

And your your Controller Method: 和您的控制器方法:

public ActionResult Index(string cust, int id, string url)     
{     
    // do some DB stuff
    return RedirectResult(url);
};

The reason your code is trying to find a cust100 action is that your URL is being matched by the default route: 您的代码尝试查找cust100操作的原因是您的URL与默认路由匹配:

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

Try adding an explicit route before this route in your global.asax file that looks like this: 尝试在global.asax文件中的此路由之前添加一个显式路由,如下所示:

routes.MapRoute(
  "Redirects",         // Route name
  "redirects/{foo}",  // URL with parameters
  new { controller = "Redirect", action = "Redirect", foo = "" }
); 

This will map any URL of the form /redirects/abc123 to the RedirectController.Redirect(string foo) method, and pass in abc123 (or whatever) as the foo parameter. 这会将任何形式为/ redirects / abc123的URL映射到RedirectController.Redirect(string foo)方法,并将abc123(或其他内容)作为foo参数传递。

Your URL should be in the same form as the ones in your Global.asax.cs . 您的URL的格式应与Global.asax.cs中的URL相同。

Ie

"{controller}/{action}/..."

So your URL probably needs to look more like: 因此,您的网址可能需要更像:

"Redirect/ToCustomer/123"

Where Redirect is the controller, ToCustomer is an action method on said controller, and "123" is the "id" parameter supplied to the action method: 其中Redirect是控制器, ToCustomer是该控制器上的操作方法,“ 123”是提供给该操作方法的“ id”参数:

public class CustomerController : Controller
{
    public ActionResult ToCustomer(int id)
    {
        ...
    }
}

On the other hand, why not just give them the url for the Detail method on your CustomerController . 另一方面,为什么不只在CustomerController上为他们提供Detail方法的URL。 Ie: 即:

"http://www.temp.com/Customer/Detail/123"

You will have to use "RedirectResult" which represents a redirection to a new URL. 您将必须使用“ RedirectResult”,它表示对新URL的重定向。

return RedirectResult(url); 返回RedirectResult(url);

This should solve your problem. 这应该可以解决您的问题。

The following listed types are the available derivations of ActionResult: 以下列出的类型是ActionResult的可用派生:

1 ContentResult —Represents a text result 1 ContentResult —表示文本结果

2 EmptyResult —Represents no result 2EmptyResult —无结果

3 FileResult —Represents a downloadable file (abstract class) 3 FileResult —表示可下载的文件(抽象类)

4 FileContentResult —Represents a downloadable file (with the binary content) 4 FileContentResult —表示一个可下载文件(带有二进制内容)

5 FilePathResult —Represents a downloadable file (with a path) 5 FilePathResult —表示一个可下载文件(带有路径)

6 FileStreamResult —Represents a downloadable file (with a file stream) 6 FileStreamResult —表示可下载文件(带有文件流)

7 HttpUnauthorizedResult —Represents the result of an unauthorized HTTP request 7 HttpUnauthorizedResult —表示未经授权的HTTP请求的结果

8 JavaScriptResult —Represents a JavaScript script 8 JavaScriptResult —表示一个JavaScript脚本

9 JsonResult —Represents a JavaScript Object Notation (JSON) result that can be used in an AJAX application 9 JsonResult —表示可在AJAX应用程序中使用的JavaScript对象表示法(JSON)结果

10 RedirectResult —Represents a redirection to a new URL 10 RedirectResult-表示重定向到新URL

11 RedirectToRouteResult —Represents a result that performs a redirection given a route values dictionary 11 RedirectToRouteResult —表示在给定路由值字典的情况下执行重定向的结果

12 PartialViewResult —Base class used to send a partial view to the response 12 PartialViewResult —用于将部分视图发送到响应的基类

13 ViewResult —Represents HTML and markup 13 ViewResult —表示HTML和标记

14 ViewResultBase —Base class used to supply the model to the view and then render the view to the response 14 ViewResultBase —用于将模型提供给视图,然后将视图呈现给响应的基类

15 XmlResult —Action result that serializes the specified object into XML and outputs it to the response stream (provided by the MvcContrib library) 15 XmlResult —将指定对象序列化为XML并将其输出到响应流的操作结果(由MvcContrib库提供)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM