ASP.NET MVC2 使用通配符或自由文本 url 自定义路由

发布于 2024-09-25 19:18:33 字数 610 浏览 0 评论 0原文

我需要向 asp.net mvc2 网站添加特定功能以提供额外的 SEO 功能,如下所示:

传入的 URL 是纯文本,可能包含如下句子

“http://somesite.com/welcome-到我们的网站”或 “http://somesite.com/cool things/check-out-this-awesome-video”

在 MVC 管道中,我想获取此 URL,去掉网站名称,在数据库表中查找剩余部分并根据表中数据的内容调用适当的控制器/视图。所有控制器都将简单地从查找表中获取唯一的 ID 的单个参数。根据不同的 URL,可以使用不同的控制器,但这必须从数据库中获取。

如果无法解析 url,则需要提供 404 错误,如果找到 url 但已过时,则需要提供 302 重定向。

解析后的 URL 必须保留在浏览器地址栏中。

我查看了路由模型和自定义路由,但无法完全弄清楚如何使用它们来实现,因为控制器不会基于简单的路由进行预定义。我也不确定如何将 404、302 返回到标头。也许我需要一个自定义的 httpmodule 或类似的东西,但去那里超出了我的理解。

这一定是可能的......我们几年前在经典 ASP 中做到了。任何人都可以帮助提供有关如何实现这一目标的一些细节吗?

I have a requirement to add specific functionality to an asp.net mvc2 web site to provide addtional SEO capability, as follows:

The incoming URL is plain text, perhaps a containing a sentence as follows

"http://somesite.com/welcome-to-our-web-site" or
"http://somesite.com/cool things/check-out-this-awesome-video"

In the MVC pipeline, I would like to take this URL, strip off the website name, look up the remaining portion in a database table and call an appropriate controller/view based on the content of the data in the table. All controllers will simply take a single parameter bieng the unique id from the lookup table. A different controller may be used depnding on different urls, but this must be derieved from the database.

If the url cannot be resolved a 404 error needs to be provided, if the url is found but obsolete then a 302 redirect needs to be provided.

Where the url is resolved it must be retained in the browser address bar.

I have had a look at the routing model, and custom routing and can't quite work out how to do it using these, as the controller would not be predefined, based on a simple route. I am also unsure of what to do to provide 404, 302 back to the headers also. Perhpas I need a custom httpmodule or similar but going there went beyond my understanding.

This must be possible somehow... we did it years ago in Classic ASP. Can anyone help with some details on how to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不及他 2024-10-02 19:18:33

好吧,最简单的方法是在 url 中的某个位置添加一个 id(通常是第一个选项)。

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

在你的控制器中,你会有类似的内容,

public class HomeController {
    public ActionResult Index(int id) {
        //check database for id
        if(id_exists) {
            return new RedirectResult("whereever you want to redirect", true);
        } else {
            return new HttpNotFoundResult();
        }
    }
}

如果你不想使用 id 方法,你可以做其他类似的事情...

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

public ActionResult Index(string category, string page_name) {
    //same as before but instead of looking for id look for pagename
}

后者的问题是,您需要考虑所有类型的路线,如果您有很多与各种类型匹配的参数,这可能会变得非常困难。

这应该会让你朝着正确的方向前进。如果您需要一些说明,请告诉我,我会看看是否可以编写特定的路线来帮助您。

另外

,您可能可以做您正在寻找的事情

public ActionResult Index() {
    //Create and instance of the new controlle ryou want to handle this request
    SomeController controller = new SomeController();
    controller.ControllerContext = this.ControllerContext;
    return controller.YourControllerAction();
}

,但我不知道这样做会产生任何副作用。 ..所以这可能不是一个好主意 - 但它似乎有效。

Well, the simplest way would be to have an id somewhere in the url (usually the first option)

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

In your controller you'd have something like

public class HomeController {
    public ActionResult Index(int id) {
        //check database for id
        if(id_exists) {
            return new RedirectResult("whereever you want to redirect", true);
        } else {
            return new HttpNotFoundResult();
        }
    }
}

If you don't want to use the id method you could do something else like...

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

public ActionResult Index(string category, string page_name) {
    //same as before but instead of looking for id look for pagename
}

The problem with the latter is that you would need to account for all types of routes and it can get really difficult if you have a lot of parameters that match various types.

This should get you in the right direction. If you neeed some clarification let me know and I'll see if I can write a specific route to help you

Additional

You could probably do what you're looking for like

public ActionResult Index() {
    //Create and instance of the new controlle ryou want to handle this request
    SomeController controller = new SomeController();
    controller.ControllerContext = this.ControllerContext;
    return controller.YourControllerAction();
}

but I don't know any of the side effects by doing that...so it's probably not a good idea - but it seems to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文