我正在使用静态方法创建静态类,以帮助控制器完成工作。构建应用程序时,我收到以下错误:
错误40'
System.Web.Mvc.Controller.Content(string)
'由于其保护级别而无法访问"
知道如何解决这个问题吗?
注意: 它是一个c#mvc aplication
public static ActionResult GetAlbumJSON(AlbumVO album)
{
return Controller.Content(
JsonConvert.SerializeObject(new
{
max_car = @ABookClient.maxCharsProjecName,
trans_img = @ABookClient.Transparent_Image,
show_description = @ABookClient.Show_Product_Description,
product_type = "Album",
obj = CreateObjAlbumVO(album),
})
);
}
答案 0 :(得分:4)
内容方法受内部保护,因此您无法在控制器之外使用它。 Controller.Content Method。很可能你的静态类违反了SRP原则。让他完成他的工作(初始化,序列化......)和控制器 - 控制器的工作 - 将结果返回给客户。
protected internal ContentResult Content(string content)
它看起来像是:
public static class MyHelper
{
public static object GetAlbum(AlbumVO album)
{
return new
{
max_car = @ABookClient.maxCharsProjecName,
trans_img = @ABookClient.Transparent_Image,
show_description = @ABookClient.Show_Product_Description,
product_type = "Album",
obj = CreateObjAlbumVO(album),
};
}
}
public class AlbumController : Controller
{
public ActionResult GetAlbums(int id)
{
var album = Context.GetAlbum(id);
var convertedResult = MyHelper.GetAlbum(album);
return Json(convertedResult);
}
}
另外,我建议您查看AutoMapper来创建客户端响应对象
答案 1 :(得分:2)
我认为这对于JSON结果的视图模型是有效的,因为您确实希望将Domain模型与发送回客户端的数据分开。使用视图模型还可以为您提供在域模型和视图(JSON)之间放置此映射的适当位置,这样您就不需要委派给帮助程序类。
<?php include('header.php')?>
<?php
if(isset($_POST['forgotpassword']))
{
if($_POST['emailz'] != '')
{
$exsistsUser = mysql_fetch_array(mysql_query("SELECT * FROM users where email='".$_POST['emailz']."'"));
if($exsistsUser['email']!='')
{
$chkresult = mysql_fetch_array(mysql_query("SELECT * FROM settings where name='contact_us_email'"));
$from = $chkresult['value'];
$mail_to = $_POST['emailz'].", ";
$headers = 'From: '.$from;
$headers = "From: " . strip_tags($from) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<table width="526" border="0" cellspacing="0" cellpadding="15">';
$message .= '<tr><td align="left" valign="top" style="border:1px dashed #393939;"><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#F5F5F5">';
$message .='<tr><td height="90" align="left" valign="middle"><div style="margin-left:15px;"><a href="'.SITE_URL.'"><img src="'.SITE_URL.'/images/reset-design-dark-top-bar_04.png" alt="reset" width="190" height="39" border="0" style="background-color: rgb(51, 51, 51);" /></a></div></td>
</tr>';
$message .="<tr>
<td height='26' align='left' valign='middle' bgcolor='#393939' style='font-family:Arial, Helvetica, sans-serif; font-size:12px; font-weight:normal; text-decoration:none; color:#FFFFFF;'><div style='margin-left:15px;'>Hi <b>'".$exsistsUser['name']."'</b></div></td>";
$message .='</tr>
<tr>
<td height="15" align="left" valign="top"></td>
</tr>';
$message .='<tr>
<td align="left" valign="top" style="font-family:Arial, Helvetica, sans-serif; font-size:12px; font-weight:normal; text-decoration:none; color:#414141;"><div style="margin-left:15px; margin-right:15px;">
<p>Thanks for registering at <a href="'.SITE_URL.'/" style="font-family:Arial, Helvetica, sans-serif; font-size:12px; font-weight:normal; text-decoration:underline; color:#00A8FF;">Reset</a>, the best social media productivity solution the web.</p>';
$message .='<p>Your details are as follows.</p>';
$message .="<p><b>User Name:</b> '".$exsistsUser['name']."'</p>";
$message .="<p><b>Email Address:</b> <span style='font-family:Arial, Helvetica, sans-serif; font-size:12px; font-weight:normal; text-decoration:underline; color:#00A8FF;'>'".$_POST['emailz']."'</span></p>";
$message .="<p><b>Password:</b> '".$exsistsUser['password']."'</p>";
$message .="<p>Please click this link to active your account</p>";
$message .="<p><a href='".SITE_URL."/confirmation.php?confirmUser=".$exsistsUserss['keyz']."'>confirm registration</a></p>";
$message .="<p><br/></p>";
$message .='<p>If you ever forget your password, you can request a new password with the Forgot Password link on the login form at <a href="'.SITE_URL.'" style="font-family:Arial, Helvetica, sans-serif; font-size:12px; font-weight:normal; text-decoration:underline; color:#00A8FF;">Reset</a> using the email address shown above.</p>
<p>Thank you for using our service.</p>
<p>Best regards,</p>
<p>Customer Service.</p>
</div></td>
</tr>';
$message .='<tr>
<td height="10" align="left" valign="top"></td>
</tr>';
$message .='</table></td></tr></table>';
$message .= "</body></html>";
echo '<div class=" icon-tick success">An e-mail with further instructions has been sent. </div>';
}
else
{
header('Location:login.php');
echo '<div class=" icon-cross alertz"> Your log-in details were invalid. Please try again. </div>';
}
}
else
echo '<div class=" icon-cross alertz">This e-mail is not registered. </div>';
}
?>
public class AlbumModel
{
[JsonProperty(PropertyName = "max_car")]
public int MaxChars { get; private set; }
[JsonProperty(PropertyName = "trans_img")]
public string TransparentImage { get; private set; }
[JsonProperty(PropertyName = "product_type")]
public string ProductType { get; private set; }
[JsonProperty(PropertyName = "obj")]
public AlbumInfo Object { get; private set; }
[JsonProperty(PropertyName = "show_description")]
public bool ShowProductDescription { get; private set; }
public AlbumModel(AlbumVO album)
{
MaxChars = album.maxCharsProjecName;
TransparentImage = album.Transparent_Image;
ShowProductDescription = album.Show_Product_Description;
ProductType = "Album";
Object = new AlbumInfo(album);
}
}
类为您的JSON结果提供了额外的映射,这将成为&#34; obj&#34;物业被送回客户。
AlbumInfo
你的控制器变得干净整洁:
public class AlbumInfo
{
// ... define properties here
public AlbumInfo(AlbumVO album)
{
// ... map properties here
}
}