如何追加字符串在asp.net mvc的网址是什么? [英] how to append string in url in asp.net mvc?

查看:73
本文介绍了如何追加字符串在asp.net mvc的网址是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景: -
我在网址以下模式: -


  

本地主机:8080 /专辑/


  routes.MapRoute(
{
  名称:AlbumHome
  网址:专辑/ {*} ALBUMNAME
  默认:新{控制器=相册,行动=索引,ALBUMNAME =}
}

现在在我的行动,我从数据库中获取ALBUMNAME,现在我应该怎么追加ALBUMNAME在URL中。

我想要的网址是为: -


  

本地主机:8080 /专辑/印地文

  本地主机:8080 /专辑/加拿大

  等


动作

 公众的ActionResult GetAlbumName()
{
  //从数据库得到ALBUMNAME
   返回RedirectToRoute(AlbumHome,ALBUMNAME);
}公众的ActionResult指数(字符串ALBUMNAME)
{
   返回视图();
}

如何ALBUMNAME这个URL中添加?


解决方案

您得到了它差不多吧!

有一个<一个href=\"https://msdn.microsoft.com/en-us/library/system.web.mvc.controller.redirecttoroute(v=vs.118).aspx#M:System.Web.Mvc.Controller.RedirectToRoute%28System.String,System.Object%29\"相对=nofollow>为 RedirectToRoute >,它允许您添加路由值作为对象参数。在你的榜样,它看起来是这样的:

 公众的ActionResult GetAlbumName()
{
  //从数据库得到ALBUMNAME
   返回RedirectToRoute(AlbumHome,新的{ALBUMNAME});
}

您还可以更明确地定义你这样的路线:

  routes.MapRoute(
{
  名称:AlbumHome
  网址:专辑/ {} ALBUMNAME
  默认:新{控制器=相册,行动=索引,ALBUMNAME = UrlParameter.Optional}
}

Scenario:- I have url in the following pattern:-

localhost:8080/albums/

routes.MapRoute(
{
  name: "AlbumHome",
  url : "Albums/{*albumName}"
  defaults: new {controller = "Albums", action="Index", albumName = ""}
}

Now in my action, I am getting the albumName from the DB, now how should I append the albumName in the url.

I want the url to be as:-

localhost:8080/albums/hindi
localhost:8080/albums/kanada
and so on.

Action

public ActionResult GetAlbumName()
{
  //get the albumName from db
   return RedirectToRoute("AlbumHome",albumName);
}

public ActionResult Index(string albumName)
{
   return view();
}

How to append this albumName in url?

解决方案

You got it almost right!

There is an overload for RedirectToRoute which allows you to add route values as an object parameter. In your example it would look like this:

public ActionResult GetAlbumName()
{
  //get the albumName from db
   return RedirectToRoute("AlbumHome", new { albumName });
} 

You could also define your route more explicitly like this:

routes.MapRoute(
{
  name: "AlbumHome",
  url : "Albums/{albumName}"
  defaults: new {controller = "Albums", action="Index", albumName = UrlParameter.Optional }
}

这篇关于如何追加字符串在asp.net mvc的网址是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆